zoukankan      html  css  js  c++  java
  • LeetCode: Two Sum

    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 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 int[] twoSum(int[] numbers, int target) {
           int[] result = new int[]{0, 0};
            int length = numbers.length;
            int i = 0, j = 0;
            for(; i < length-1; i++)
            {
                for(j = i + 1; j < length; j++)
                {
                    if (numbers[i] + numbers[j] == target)
                    {
                        result[0] = i + 1;
                        result[1] = j + 1;
                    }
                }
            }
            return result; 
        }

    结果超时,只能说你不支持o(n^2)了*_*

    之后上网查,发现了可以使用hashtable<numbers[i], i>,将全部数字装入table,并在遍历的过程中,查看:

    n = table.get(target - numbers[i]);
                
    if (n != null && n < i)
        {
            result[0] = n + 1;
            result[1] = i + 1;
        }

    然后返回result数组即可。

    accept之后,我想能不能使用二分查找呢?

    先前假设numbers[]是已经排序好的,并且木有重复元素,遍历数组,查找(target-numbers[i]),找到后直接返回,跳出遍历过程。

    for(int i = 0; i < length; i++)
            {
                int j = binarySearch(numbers, target - numbers[i]);
                
    //            System.out.println("jjjjjjjj + 1: " + j);
                
                if(j != -1)
                {
                    result[0] = i + 1;
                    result[1] = j + 1;
                    break;
                }
            }
            
            return result;

    二分查找部分:

    public int binarySearch(int[] numbers, int target)
        {
            int low = 0, high = numbers.length-1;
            int mid;
            while(low <= high)
            {
                mid = low + (high - low) / 2;
                if (target > numbers[mid])
                {
                    low = mid+1;
                }
                else if (target < numbers[mid])
                {
                    high = mid-1;
                }
                else 
                {
                    return mid;
                }
            }
            return -1;
            
        }

    尽管代码完成了,但是是错误的,提供的numbers数组并不是已经排序好的,当然还有重复元素*_*

    Input:     [0,4,3,0], 0
    Output:     1, 1
    Expected:     1, 4

    复杂度的话,应该没问题,第一段遍历(o(n)),二分查找是(o(logn)),o(n*logn)这个必须支持吧!!!

  • 相关阅读:
    数据恢复:解决ORA600[kghstack_free2][kghstack_err+0068]一例
    Oracle latch闩原理示意图
    MySQL Query Analyzer查询分析器
    Oracle GoldenGate Monitor架构图
    Oracle Row cache lock图解
    没有Metalink账号的同学可以观赏下,My Oracle Support的主界面
    Oracle Goldengate Director软件截面图
    Oracle Unbreakable Enterprise Kernel Faster than Redhat?
    从Win32过渡到MFC
    naked 函数调用
  • 原文地址:https://www.cnblogs.com/dslover/p/4308972.html
Copyright © 2011-2022 走看看