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. dotnet core 出现Can not find runtime target for framework '.NETCoreApp,Version=v1.6' 的解决办法

    如果你在更新dotnet core新的类库后运行程序提示如下的错误: Can not find runtime target for framework '.NETCoreAPP, Version=v ...

  2. reveal

    链接 界面调试工具Reveal Reveal使用教程 iOS分析UI利器——Reveal及简单破解方法 Reveal使用步骤和 破解Revealapp的试用时间限制 end

  3. sql server 导出表结构到 word

    ------导出表结构语句1.执行以下查询 SELECT    表名       = case when a.colorder=1 then d.name else '' end,    表说明    ...

  4. Python-13-堡垒机开发

    今天主要是熟悉堡垒机开发流程.

  5. LUA类

    cpp_object_map = {}setmetatable(cpp_object_map, { __mode = "kv" }) local search_basesearch ...

  6. async/await Task Timeout

    async/await Task Timeout 在日常的电脑使用过程中,估计最难以忍受的就是软件界面"卡住""无响应",在我有限的开发生涯中一直都是在挑战 它 ...

  7. golang使用yaml格式解析构建配置文件

    现在主流的配置文件格式有这么几种,xml.yaml.config…  xml就算了,太挫了,太土, 太繁琐… config 就是mysql,apache my.cnf的那种格式,这个格式适合功能分层, ...

  8. 基本排序算法的Python实现

    本篇主要实现九(八)大排序算法,分别是冒泡排序,插入排序,选择排序,希尔排序,归并排序,快速排序,堆排序,计数排序.希望大家回顾知识的时候也能从我的这篇文章得到帮助. 为了防止误导读者,本文所有概念性 ...

  9. 文件操作之FileOpenPicker、FileSavePicker和FolderPicker

    Win10的开发经常需要进行文件的操作,因此文件的选择对话框FileOpenPicker.文件保存对话框FileSavePicker以及文件夹选择对话框FolderPicker十分重要.这三者的操作也 ...

  10. signalr服务端-基础搭建

    signalr 支持 iis托管.winform.windowsservices.wpf 托管 这里我采用winfrom托管 首先画一个这样的窗体 在服务项目通过项目管理包安装signalr类库 安装 ...