您的位置:首页 > 其它

LeetCode: Two Sum

2013-05-12 22:11 393 查看
http://leetcode.com/onlinejudge#question_1
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

JAVA版答案:

两个for循环,O(N^2).需要改进 Judge Small 496 milli secs, Judge Large 540 milli secs.n不大的时候这样很好。

public class Solution {
public int[] twoSum(int[] numbers, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int[] arr = new int[2];
for (int i = 0; i <= numbers.length - 2; ++i) {
for (int j = i + 1; j <= numbers.length - 1; ++j) {
if (numbers[i] + numbers[j] == target) {
arr[0] = i + 1;
arr[1] = j + 1;
break;
}
}
}
return arr;
}
}


从今天开始做leetcode准备找饭碗混口饭吃免得饿死了。。。

C++版答案:

两个for循环,O(N^2).需要改进 Judge Small 16 secs, Judge Large 52 milli secs.

class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> ivct;
for (int i = 0; i <= numbers.size() - 2; ++i) {
for (int j = i + 1; j <= numbers.size() - 1; ++j) {
if (numbers[i] + numbers[j] == target) {
ivct.push_back(i + 1);
ivct.push_back(j + 1);
break;
}
}
}
return ivct;
}
};


C++改进:先排序o(nlogn),在头尾往中间找o(n) Judge Smal 8 milli secs, Judge Large 12 milli secs.这显然是最高效的算法了,速度大大提升。

struct Node
{
int val;
int index;
Node(){}
Node(int v, int idx):val(v), index(idx){}
};

bool compare(const Node &lhs, const Node &rhs)
{
return lhs.val < rhs.val;
}

class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<Node> a;
for(int i = 0; i < numbers.size(); i++)
a.push_back(Node(numbers[i], i + 1));
sort(a.begin(), a.end(), compare);

int i = 0;
int j = numbers.size() - 1;
while(i < j)
{
int sum = a[i].val + a[j].val;
if (sum == target)
{
vector<int> ret;
int minIndex = min(a[i].index, a[j].index);
int maxIndex = max(a[i].index, a[j].index);
ret.push_back(minIndex);
ret.push_back(maxIndex);
return ret;
}
else if (sum < target)
i++;
else
j--;
}
}
};


JAVA改进,nlogn,(已验证)Judge Small 556 milli secs,Judge Large 524 milli secs......与原版相比,小数据更慢,大数据更快,因为这是对n很大时候的高效算法。

import java.util.Arrays;

//author:pz

/* 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.
*/

class Node implements Comparable<Node> {
private int val;
private int index;

Node(int v, int idx) {
setVal(v);
setIndex(idx);
}

public int getVal() {
return this.val;
}

public void setVal(int val) {
this.val = val;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

@Override
public int compareTo(Node o) {
// TODO Auto-generated method stub
return this.getVal() - o.getVal();
}

public String toString() {
return Integer.toString(getVal());
}
}

public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] ret = new int[2];
Node[] a = new Node[numbers.length];
for (int i = 0; i < numbers.length; i++) {
a[i] = new Node(numbers[i], i + 1);
}
Arrays.sort(a);
int i = 0, j = a.length - 1;
LABLE: while (i < j) {
int sum = a[i].getVal() + a[j].getVal();
if (sum == target) {
ret[0] = Math.min( a[i].getIndex(), a[j].getIndex());
ret[1] = Math.max( a[i].getIndex(), a[j].getIndex());
break LABLE;
} else if (sum < target)
++i;
else
--j;
}
return ret;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: