LeetCode 算法实现:TwoSum

问题:LeetCode1:TwoSum

Given an array of integers, find two numbers such that they addup to a specific target number.

The function twoSum should return indices of the two numberssuch 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 notzero-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


算法思想:

用一般的方法算法的复杂度较高,我想到一个简单的思路:

    首先,对给出的数组升序排序(降序也可以,整体倒过来也行)。时间复杂度最快为lgN。然后,折半找数组中的数据,记住最后一个比给出的和小的那个位置b(实际上应该是target减去数组的第一个数,详细解释在代码中)。从这个位置开始向前找,利用快速排序的思想从0到b之间找两个数,使之和等于target。


这个方法实现的程序(C#实现)通过了LeetCode检测,提示用了16个测试,用时492ms,超过了75%的人。


希望谁还有更好的方法拿出啦分享,大家一起进步。

下载地址:http://download.csdn.net/detail/worsun/9119589

不想下载的下面也有一样的代码。

代码:

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        int[] temp = (int [])nums.Clone();//保存原顺序 注意:这里要求深拷贝,不能是浅拷贝,直接赋值是浅拷贝
            int[] tempResult = new int[] { 0, 0};
            int[] result = new int[] { 0,0};
            Array.Sort(nums);
            int pre = 0;

之所以用target-nums[0]是因为有可能nums[0]为负数,两个数相加更小了,造成不准确。所以要减去nums[0]
            int rear = FindLocation(nums, 0, nums.Length, target - nums[0]);
            //if(nums[rear] == target && nums[0] == 0)//如果找到rear恰好等于target,由于数组有序,所以只要找数组的第一个数是不是等于0即可
            //{
            //    result[0] = 1;
            //    result[1] = rear + 1;
            //    return result;
            //}
            while(pre < rear)//采用快速排序的想法,从两边进行
            {
                while(nums[pre] + nums[rear] < target)
                {
                    if(pre == rear)//如果两个数相等后,nums[pre] + nums[rear] < target还成立的话,那么不存在符合条件的情况,跳出循环
                    {
                        return tempResult;
                    }
                    pre++;
                }
                if(nums[pre] + nums[rear] == target)
                {
                    tempResult[0] = pre;
                    tempResult[1] = rear;
                    break;
                }
                rear--;
            }
            for(int i = 0, j = 0; i < temp.Length; i++ )
            {
                if(nums[tempResult[0]] == temp[i] || nums[tempResult[1]] == temp[i])
                {
                    result[j++] = i + 1;
                }
            }
            Array.Sort(result);
            return result;
    }
    public static int FindLocation(int[] nums, int low, int hight, int target)
        {//-3,3,4,90
            if(1 == hight - low)
            {
                return low;
            }
            int location = (low + hight) / 2;
            if(target < nums[location])
            {
                return FindLocation(nums, low, location, target);
            }
            else if(target == nums[location])
            {
                return location;
            }
            else
            {
                return FindLocation(nums, location, hight, target);
            }
        }
}

下载地址:http://download.csdn.net/detail/worsun/9119589

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值