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);
        }
    }
  • 相关阅读:
    安装wampserver2时出现的问题
    微信相关信息
    YII CDbCriteria总结
    discuz@功能的代码
    音乐搜索并生成播放功能
    php生成json和js解析json
    Discuz!提取文章标签
    ⑦ vue项目结构study
    ⑤ elementui 使用字符填充table空白表格项
    ④ keep-alive缓存组件,操作之后需要重新获取数据--activated
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7837150.html
Copyright © 2011-2022 走看看