zoukankan      html  css  js  c++  java
  • leetCode-Two Sum II

    Description:
    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.

    Example:

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

    My Solution:

    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int index1 = 0;
            int index2 = 0;
            int len = numbers.length;
            int temp = target/2;
            for(int i = 0;i < len -1;i++){
                if(numbers[i] > temp){
                    break;
                }
                for(int j = i + 1;j < len;j++){
                    if((numbers[i] + numbers[j]) > target){
                        break;
                    }
                    if((numbers[i] + numbers[j]) == target){
                        index1 = i;
                        index2 = j;
                        return new int[]{index1 + 1,index2 + 1};
                    }
                }
            }
            return new int[]{index1 + 1,index2 + 1};
        }
    }
    
    
    Better Solution(方法1):
    
    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int[] indices = new int[2];
            if (numbers == null || numbers.length < 2) {
                return indices;
            }
            int left = 0, right = numbers.length - 1;
            while (left < right) {
                int v = numbers[left] + numbers[right];
                if (v == target) {
                    indices[0] = left + 1;
                    indices[1] = right + 1;
                    break;
                } else if (v > target) {
                    right--;
                } else {
                    left++;
                }
            }
            return indices;
        }
    }
    Best Solution(方法2):
    
    class Solution {
        public int[] twoSum(int[] numbers, int target) {
            if (numbers == null || numbers.length == 0) {
                return new int[2];
            }
            int start = 0;
            int end = numbers.length - 1;
            while (start < end) {
                if (numbers[start] + numbers[end] == target) {
                    return new int[]{start + 1, end + 1};
                } else if (numbers[start] + numbers[end] > target) {
                    // move end forward to the last value that numbers[end] <= target - numbers[start]
                    end = largestSmallerOrLastEqual(numbers, start, end, target - numbers[start]);
                } else {
                    // move start backword to the first value that numbers[start] >= target - numbers[end]
                    start = smallestLargerOrFirstEqual(numbers, start, end, target - numbers[end]);
                }
            }
            return new int[2];
        }
        private int largestSmallerOrLastEqual(int[] numbers, int start, int end, int target) {
            int left = start;
            int right = end;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (numbers[mid] > target) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            return right;
        }
        private int smallestLargerOrFirstEqual(int[] numbers, int start, int end, int target) {
            int left = start;
            int right = end;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (numbers[mid] < target) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            return left;
        }
    }

     
    方法1利用了数组nums递增的特点,设置两个指针,left和right,分别表示数组的开始和结尾,开始迭代,如果nums[left] + nums[right] > target,那么right左移,反之,left右移

    方法2对方法1的改进在于,方法一在做出判断时,只让left或者right往右移或者往左移一步,而方法2通过largestSmallerOrLastEqual()方法让right最大限度移动(如果找到则返回,如果没找到,那么对当前start,在nums中找不到

    nums[start] + nums[end] = target,于是用二分查找,将end置为可以target可以在nums中插入的位置的前一位,那么下次进入smallestLargerOrFirstEqual(),smallestLargerOrFirstEqual()与largestSmallerOrLastEqual()类似,

    只是换成了将start最大限度往右移。如此循环,找到nums[start] + nums[end] = target,返回。

    版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    python正则表达式re模块
    链表算法题之中等级别,debug调试更简单
    链表算法题二,还原题目,用debug调试搞懂每一道题
    开启算法之路,还原题目,用debug调试搞懂每一道题
    K8S线上集群排查,实测排查Node节点NotReady异常状态
    手写单链表基础之增,删,查!附赠一道链表题
    kafka初识
    docker之mysql镜像使用
    CS61B sp2018笔记 | Lists
    JSONArray.fromObject不执行且不报错问题的解决
  • 原文地址:https://www.cnblogs.com/kevincong/p/7881379.html
Copyright © 2011-2022 走看看