LeetCode--数组II

作者: Jun_简书 | 来源:发表于2017-08-22 14:26 被阅读1次

给一个有序数组和一个目标值,找到数组中有多少对它们的总和大于特定目标数。请返回成对数。
Example
numbers=[2, 7, 11, 15], target=24
return 1

- (NSInteger)getArray:(NSArray<NSString *> *)array target:(NSInteger)target {
    
    NSInteger count = 0;
    NSInteger left = 0;
    NSInteger right = array.count - 1;
    while (left < right) {
        if ([array[left] integerValue] + [array[right] integerValue] > target) {
            count += (right - left);
            
            right--;
        } else {
            left++;
        }
    }
    
    return count
}

相关文章

网友评论

    本文标题:LeetCode--数组II

    本文链接:https://www.haomeiwen.com/subject/kgkldxtx.html