zoukankan      html  css  js  c++  java
  • leetcode167- 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 and you may not use the same element twice.

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

    用hashset要O(n)空间,这题可以O(1)空间+O(n)时间。

    1.O(n)时间+O(1)空间:两个指针往中间移动,通过加和跟target的大小对比来决定动哪根指针。

    2.O(nlogn)时间 + O(1)空间:顺序扫描+binary search

    3.O(n)时间+O(n)空间:hashset法

    1.指针法

    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            
            int[] result = new int[2];
            if (numbers == null || numbers.length == 0) {
                return result;
            }
            
            int start = 0;
            int end = numbers.length - 1;
            while (start < end) {
                int sum = numbers[start] + numbers[end];
                if (sum == target) {
                    result[0] = start + 1;
                    result[1] = end + 1;
                    return result;
                } else if (sum < target) {
                    start++;
                } else {
                    end--;
                }
            }
            return result;
        }
    }

    2.O(nlogn)的扫描+BS实现:

    class Solution {
        private class ResultType{
            public boolean hasCp;
            public int idx;
            public ResultType(boolean hasCp, int idx) {
                this.hasCp = hasCp;
                this.idx = idx;
            }
        }
        
        public int[] twoSum(int[] numbers, int target) {
            
            int[] result = new int[2];
            for (int i = 0; i < numbers.length; i++) {
                ResultType rt = BS(numbers, i + 1, target - numbers[i]);
                if (!rt.hasCp) {
                    continue;
                }
                result[0] = i + 1;
                result[1] = rt.idx + 1;
                return result; 
            }
            // return idx + 1
            result[0] = result[1] = -1;
            return result;
        }
        
        private ResultType BS(int[] numbers, int startIdx, int target) {
            int start = startIdx;
            int end = numbers.length - 1;
            while (start + 1 < end) {
                int mid = start + (end - start) / 2;
                if (target <= numbers[mid]) {
                    end = mid;
                } else {
                    start = mid;
                }
            }
            if (numbers[start] == target) {
                return new ResultType(true, start);
            }
            if (numbers[end] == target) {
                return new ResultType(true, end);
            }
            return new ResultType(false, -1);
        }
    }
  • 相关阅读:
    HDU 4489 The King’s Ups and Downs (DP+数学计数)
    HDU 2084 数塔 (水DP)
    CodeForces 706C Hard problem (水DP)
    CodeForces 706B Interesting drink (二分查找)
    CodeForces 706A Beru-taxi (数学计算,水题)
    HDU 4438 Hunters (数学,概率计算)
    机器学习-基础知识
    如何学习-学习理论(A-1)-KOLB学习周期理论
    如何学习-辅助工具
    如何学习-入门之后
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7837150.html
Copyright © 2011-2022 走看看