zoukankan      html  css  js  c++  java
  • leetcode — two-sum-ii-input-array-is-sorted

    import java.util.Arrays;
    
    /**
     * Source : https://oj.leetcode.com/problems/two-sum-ii-input-array-is-sorted/
     *
     * Created by lverpeng on 2017/6/22.
     *
     * 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.
     *
     * Input: numbers={2, 7, 11, 15}, target=9
     * Output: index1=1, index2=2
     *
     */
    public class TwoSum2 {
        public static void main(String[] args) {
            TwoSum2 twoSum2 = new TwoSum2();
            int[] numbers = {15, 7, 2, 11};
            twoSum2.quicksort(numbers, 0, 3);
            System.out.println(Arrays.toString(numbers));
            System.out.println(Arrays.toString(twoSum2.twoSum(numbers, 9)));
        }
    
        /**
         * 相比于一,这个问题里面的输入数组是有序的,可以使用两个指针来分别指向数组的首尾,
         * 根据当前指针指向的两个数的和与target的比较来决定那一个指针移动
         * 时间复杂度:O(n),因为这里已经排好序了,如果还要排序的话,排序最佳时间复杂度是O(nlogn),quicksort
         * 空间复杂度:O(1)
         *
         * @param numbers
         * @param target
         */
        public int[] twoSum (int[] numbers, int target) {
            int[] result = new int[2];
            int low = 0;
            int high = numbers.length - 1;
    
            while (low < high) {
                if ((numbers[low] + numbers[high]) == target) {
                    result[0] = low + 1;
                    result[1] = high + 1;
                    break;
                } else if ((numbers[low] + numbers[high]) > target) {
                    high--;
                } else {
                    low++;
                }
            }
            return result;
        }
    
        /**
         * 快速排序
         * 时间复杂度:平均O(nlogn),最坏O(n^2)
         * 基本思路:
         * 1. 取出一个基准,通常取最后一个元素
         * 2. 定义首尾两个指针,根据基准将数组分成,左边是小于基准,右边大于基准
         * 3. 递归的对左边和右边,执行步骤1、2
         *
         * @param nums
         * @param start
         * @param end
         */
        public void quicksort (int[] nums, int start, int end) {
            if (start >= end) {
                return ;
            }
            int left = start;
            int right = end;
            int mid = nums[end];
            while (left < right) {
                while (nums[left] <= mid && left < right) {
                    left++;
                }
                while (nums[right] >= mid && left < right) {
                    right--;
                }
                swap(nums, left, right);
            }
            if (nums[left] >= mid) {
                swap(nums, left, end);
            } else {
                left++;
            }
            quicksort(nums, start, left - 1);
            quicksort(nums, left, end);
        }
    
        public void swap (int[] arr, int x, int y) {
            int temp = arr[x];
            arr[x]  =arr[y];
            arr[y] = temp;
        }
    }
    
  • 相关阅读:
    滚屏到相应位置才加载图片
    cs文件编译成dll文件
    SPAM搜索引擎垃圾技术
    新HTML元素:Canonical Link(权威链接),解决网络复制内容
    Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
    VS2005发布网站项目时整个站点生成为一个自定义名称的dll文件
    301转向代码合集
    区别不同浏览器,CSS hack写法
    教你识别交换购买友情链接里的八种骗局真相!
    C#中Cookies的存取
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7226555.html
Copyright © 2011-2022 走看看