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. 第4章 Java接收用户输入

    第4章 Java接收用户输入 1.输入 使用Scanner工具类可以换取用户输入的数据Scanner类位于java.util包中,使用时需要导入此包使用步骤: 1.导入java.util.Scanne ...

  2. python高级之函数

    python高级之函数 本节内容 函数的介绍 函数的创建 函数参数及返回值 LEGB作用域 特殊函数 函数式编程 1.函数的介绍 为什么要有函数?因为在平时写代码时,如果没有函数的话,那么将会出现很多 ...

  3. XCodeo如何去除多余的模拟器---学习笔记七

    首先退出Xcode并且关闭模拟器: 然后在终端(Terminal)输入如下2行命令: sudo killall -9 com.apple.CoreSimulator.CoreSimulatorServ ...

  4. CSS常用浮出层的写法

    点此查看实例展示 是的,我们即将实现的就是以上功能,是不是很生动? 贴上HTML: <div class="poptip"> <span class=" ...

  5. jQuery的一些常用的方法(转载)

    操作元素的样式 主要包括以下几种方式: $("#msg").css("background"); //返回元素的背景颜色 $("#msg") ...

  6. windows7配置Nginx+php+mysql教程

    windows7配置Nginx+php+mysql教程 最近在学习php,想把自己的学习经历记录下来,并写一些经验,仅供参考交流.此文适合那些刚刚接触php,想要学习并想要自己搭建Nginx+php+ ...

  7. 分布式光伏系列:分布式光伏电站 运行与维护方案一览(zz)

    原文:http://www.toutiao.com/a6353487210709516546/ 中小型光伏电站的特点是占地面积小.安装位置灵活且日常维护量少.由于光伏电站不同的运行环境,为了能够使光伏 ...

  8. JS组件系列——Bootstrap右键菜单解决方案:ContextMenu

    前言:有段时间没发表随笔了,过个年人都变得懒了.新年刚来上班,今天正好得空,将去年遗留的两个小组件总结记录下.有朋友跟我说:你的bootstrap组件要能够形成一个可以满足一般项目需求的系列组件,才有 ...

  9. 云计算之路-阿里云上:10:28-10:51云盾清洗以及IP切换引发的主站访问故障

    大家好,非常抱歉!今天10:28-10:51期间由于阿里云云盾流量清洗,以及切换IP后负载均衡的带宽跑满,影响了主站的正常访问,给您造成了很大的麻烦,请您谅解! 故障的过程是这样的: 10:28,我们 ...

  10. UML类图详解

    下面是类图的实例: (注:飞翔接口那里应为空心三角形) UML中类图实例 接口:空心圆+直线(唐老鸭类实现了‘讲人话’):依赖:虚线+箭头(动物和空气的关系):关联:实线+箭头(企鹅需要知道气候才迁移 ...