zoukankan      html  css  js  c++  java
  • lintcode56

    Given an array of integers, 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 zero-based.
    Example
    numbers=[2, 7, 11, 15], target=9
    return [0, 1]
    Challenge
    Either of the following solutions are acceptable:
    * O(n) Space, O(nlogn) Time
    * O(n) Space, O(n) Time
    Notice
    You may assume that each input would have exactly one solution
     
     
    1.O(n) Space, O(n) Time:  用HashMap. 存期待值和当前下标。
    2.O(n) Space, O(nlogn) Time: 用相向双指针。先排序,循环里每次求和对比target,如果小了就右移左指针(可以想到此时左指针和所有右指针左边数的组合都是不可能的了,所以排除),如果大了就左移右指针。
     
    细节:
    1.双指针写法中注意在要求返回index的情况下你必须要用私有类Pair同时存储数值和下标,并且要自己写Comparator。为了解决排序后的index和原始index已经不一样了的问题。
    2.comparator要点:
        private class numComparator implements Comparator<Pair> {
            @Override
            public int compare(Pair a, Pair b) {
                return a.num - b.num;
            }
        }
    class, <>, Override大写O,compair正常序写return就按顺序,调用写Arrays.sort(nums, new numComparator());
    3.数组初始化要点:{}写法只放在初始化里,固定的int[] result = {-1, -1},其他你能想到的return啊赋值啊基本上都是编译错误的不要乱用。 
     
     
    实现1 HashMap:
    public class Solution {
        /*
         * @param numbers : An array of Integer
         * @param target : target = numbers[index1] + numbers[index2]
         * @return : [index1 + 1, index2 + 1] (index1 < index2)
             numbers=[2, 7, 11, 15],  target=9
             return [1, 2]
         */
        public int[] twoSum(int[] numbers, int target) {
            HashMap<Integer,Integer> map = new HashMap<>();
    
            for (int i = 0; i < numbers.length; i++) {
                if (map.get(numbers[i]) != null) {
                    int[] result = {map.get(numbers[i]), i};
                    return result;
                }
                map.put(target - numbers[i], i);
            }
            
            int[] result = {};
            return result;
        }
    }
     
    实现2 双指针
    public class Solution {
        /**
         * @param numbers: An array of Integer
         * @param target: target = numbers[index1] + numbers[index2]
         * @return: [index1, index2] (index1 < index2)
         */
         
        private class Pair {
            int num;
            int index;
            public Pair(int num, int index) {
                this.num = num;
                this.index = index;
            }
        }
        
        private class numComparator implements Comparator<Pair> {
            @Override
            public int compare(Pair a, Pair b) {
                return a.num - b.num;
            }
        }
        
        public int[] twoSum(int[] numbers, int target) {
            // write your code here
            int[] result = {-1, -1};
            if (numbers == null) {
                return result;
            }
            
            Pair[] nums = new Pair[numbers.length];
            for (int i = 0; i < numbers.length; i++) {
                nums[i] = new Pair(numbers[i], i);
            }
            
            Arrays.sort(nums, new numComparator());
            int left = 0, right = nums.length - 1;
            while (left < right) {
                int sum = nums[left].num + nums[right].num;
                if (sum == target) {
                    result[0] = Math.min(nums[left].index, nums[right].index);
                    result[1] = Math.max(nums[left].index, nums[right].index);
                    return result;
                } else if (sum > target) {
                    right--;
                } else {
                    left++;
                }
            }
            return result;
        }
    }
  • 相关阅读:
    了解动态调试smali
    cli命令
    pass
    cs
    dsp查看
    云翌端口映射
    修改时间
    crm地址修改
    [autocallcustome]湖州瑞声科技有限公司
    播放语音文件
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7498388.html
Copyright © 2011-2022 走看看