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);
        }
    }
  • 相关阅读:
    Vue 全局过滤器和局部过滤器使用方式
    Vue 标签显示/隐藏的方式对比demo
    OpenSSL SSL_read: Connection was reset, errno 10054
    Vue 自定义全局指令和局部指令demo
    Vue 列表渲染demo
    Vue Class样式和style样式编写
    vue中的函数(methods),计算属性(computed),监听器(watch)的区别demo
    Vue 常用指令
    针对 WebSocket 协议的 Locust 压测脚本实现(基于 Locust 1.0 以上版本)
    Locust 脚本开发入门(4)
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7837150.html
Copyright © 2011-2022 走看看