zoukankan      html  css  js  c++  java
  • 167. Two Sum II

    题目:

    Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    链接: http://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

    题解:

    排序好的数组求two sum。用头尾两个指针对着夹逼一下就可以了。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int[] res = {-1, -1};
            if(numbers == null || numbers.length == 0)
                return res;
            int lo = 0, hi = numbers.length - 1;
            
            while(lo < hi) {
                if(numbers[lo] + numbers[hi] < target)
                    lo++;
                else if(numbers[lo] + numbers[hi] > target)
                    hi--;
                else {
                    res[0] = lo + 1;
                    res[1] = hi + 1;
                    return res;
                }
            }
            
            return res;
        }
    }

    二刷:

    和一刷一样,双指针夹逼

    Java:

    public class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int[] res = {-1, -1};
            if (numbers == null || numbers.length == 0) {
                return res;
            }
            int lo = 0, hi = numbers.length - 1;
            while (lo <= hi) {
                int sum = numbers[lo] + numbers[hi];
                if (sum < target) {
                    lo++;
                } else if (sum > target) {
                    hi--;
                } else {
                    res[0] = lo + 1;
                    res[1] = hi + 1;
                    return res;
                }
            }
            return res;
        }
    }

    三刷:

    Java:

    public class Solution {
        public int[] twoSum(int[] nums, int target) {
            if (nums == null || nums.length < 2) return nums;
            int lo = 0, hi = nums.length - 1;
            while (lo < hi) {
                int sum = nums[lo] + nums[hi];
                if (sum == target) return new int[] {lo + 1, hi + 1};
                else if (sum < target) lo++;
                else if (sum > target) hi--;
            }
            return new int[] {-1, -1};
        }
    }

    Reference:

  • 相关阅读:
    类之OCP(Open Closed Principle):开闭原则
    OO书籍 zz
    ObjectOriented Design Heuristics (zz)
    CSS3 filter(滤镜) 网站整体变灰色调
    js和jquery设置css样式的几种方法
    20条书写CSS代码
    js 调用向html追加内容
    移动端检测微信浏览器返回,关闭,进入后台操作
    防止表单重复提交的4种方法
    CSS浮动标准修复top塌陷和清除浮动及IE兼容标准格式
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4491635.html
Copyright © 2011-2022 走看看