Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.

public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] copy = new int[nums.length];
int index1 = 0;
int index2 = nums.length - 1;
int first = -1;
int second = -1;
int[] result = new int[2];
System.arraycopy(nums, 0, copy, 0, nums.length);
Arrays.sort(copy); while (index1 < index2) {
if (copy[index1] + copy[index2] == target) {
result[0] = copy[index1];
result[1] = copy[index2];
break;
}else if(copy[index1] + copy[index2] < target){
index1++;
}else{
index2--;
}
}
for(int i = 0; i < nums.length; i++){
if (result[0] == nums[i] && first == -1){
first = i;
}else if (result[1] == nums[i] && second == -1){
second = i;
}
} result[0] = first;
result[1] = second;
return result;
}
}

Two Sum Leetcode Java的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Minimum Path Sum leetcode java

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  3. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  4. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  5. 4 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  6. 3 Sum leetcode java

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  7. Combination Sum leetcode java

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...

  8. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  9. LeetCode算法题-Path Sum(Java实现)

    这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...

随机推荐

  1. 适配iOS10的哪些事 ---- 学习笔记八

    一. 上传了一个版本,为什么没有构建版本? 解:info.plist中的Bundle version 与上一个版本一致或少于上一个版本,上线新版本时,Bundle version和Bundle ... ...

  2. sublime 插件

    由于之前的代码可视化方案太复杂,分析时间太长,不实用,另一方面是而且工作以后业余时间大大减少,因此决定放弃原有路线,从工作中最迫切的需求着手,逐步构建一个实用的工具. 新的方法仍然依赖understa ...

  3. LeetCode:Merge k Sorted Lists

    题目链接 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  4. jQuery之Ajax--底层接口

    1. $.ajax()方法:是jQuery最底层的Ajax实现.它的结构为:$.ajax(options).该方法只有一个参数,但在这个对象里面包含了$.ajax()方法所需要的请求设置以及回调函数等 ...

  5. ES6新特性:使用新方法定义javascript的Class

    ES6中定义类的方式, 就是ES3和ES5中定义类的语法糖,虽然也有些区别,但是整体定义类的方式更加简洁,类的继承更加方便, 如果想对ES6中的继承更加熟悉, 最好了解ES5中原型继承的方式, 博客园 ...

  6. DOSBOX 自动挂载技巧

    DOSBOX下载之后,win10已经不支持debug了(win-XP虚拟机有!),所以需要单独下载,每次使用都需要挂载上去,十分不方便. 解决办法是修改属性文件,每次挂载都自动执行: 把编写好的汇编文 ...

  7. 【Beta】Scrum5.5

    Info 时间:2016.12.13 21:45 时长:15min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:2016.12.15 21:30 Task Report Name ...

  8. mybatis 批量更新

    <update id="batchUpdate" parameterType="java.util.List"> <foreach colle ...

  9. IIS错误处理集合

    1.编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ya ...

  10. @SuppressWarnings的参数

    @SuppressWarnings 是J2EE的最后一个批注,该批注的作用是告诉编译器对被批注的元素内部的某些警告保持静默 @SuppressWarnings("unchecked" ...