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);
        }
    }
  • 相关阅读:
    Can't connect to X11 window server using 的问题,求解
    自动化运维,让你远离背锅侠
    python netmiko实现cisco网络设备配置备份
    如何备份思科、锐捷、Juniper的配置文件
    网络配置备份。
    使用SecureCRT脚本备份网络设备配置的一点感悟
    網管利器!開源管理系統-LibreNMS
    邮件协议与port
    视频播放技术汇总(列表播放,小窗播放,跨界面播放,播放中网络切换提示)
    android插件式开发资料整理
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7837150.html
Copyright © 2011-2022 走看看