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, and you may not use the same element twice.

Example:

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

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

Subscribe to see which companies asked this question.

Show Tags
Show Similar Problems
package TWO_SUM_001;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        int nums[] = {2,7,11,15};
int num[] = twoSum(nums,9);
print(num); } private static void print(int[] num) {
if (num != null && num.length>0) {
for (int n:num) {
System.out.print(n+" ");
}
} } public static int[] twoSum(int[] nums, int target) {
int[] array = new int[2];
//1:首先对数组中数字排序
Arrays.sort(nums);
//2:
int start = 0;//下标
int end = nums.length-1;//下标 //从两边向中间靠拢
while (start < end) {
//找到输入的结果直接返回
if (nums[start]+nums[end] == target) { if (nums[start] > nums[end]) {
array[0] = end+1;
array[1] = start+1;
} else {
array[0] = start+1;
array[1] = end+1;
}
break;
} else if (nums[start]+nums[end] > target) {//如果大于右边的值end--
end--;
} else {
start++;
}
}
return array;
} }

001-Two Sum的更多相关文章

  1. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  2. 【JAVA、C++】LeetCode 001 Two Sum

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

  3. No.001 Two Sum

    Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...

  4. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  5. LeetCode--No.001 Two Sum

    Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...

  6. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  7. 001 Two Sum 两个数的和为目标数字

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

  8. 【LeetCode】001. Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. LeetCode 【1】 Two Sum --001

    5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...

  10. 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用

    001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...

随机推荐

  1. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  2. LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)

    题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description   给出一个未排序的数组,求出第一个丢失的正数. ...

  3. sencha touch Ext.Ajax.request 错误 Cannot call method 'request' of undefined 解决方案

    凡是Cannot call method '' of undefined 这类错误大部分都可以参照下面的办法来解决 在st中有时候你会发现使用Ext.Ajax.request会出现一下错误: Cann ...

  4. ELK系列七:Elasticsearch的集群配置和监控以及在部署ELK中踩的坑

    1.基本下载安装 #按照ELK系列一博客安装启动即可,没有大坑,注意一下权限即可 chmod -R 777 ./elasticsearch #此外没有java的,注意安装下JDK,我这次部署的环境是C ...

  5. windows中cmd--->进入到别的磁盘

    方法:直接敲:  f:     不要加cd,在同一个磁盘的盘符下用cd.

  6. C# 未能加载文件或程序集“mysql.data”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    报错信息: 在web.config中已经加了以下代码. <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-co ...

  7. Linux系统java环境jdk的安装

    在linux环境中jdk的安装有两种方式,一为rpm安装机制,另一种为源码安装(已编译好)因此在ORACLE官网提供两种安装文件,一为rpm格式,另一种为gz格式,两种的安装方式都大同小异的. 1.r ...

  8. iOS - Block产生Memory Leaks循环引用导致的内存泄漏以及解决方案

    在ARC(自动引用技术)前,Objective-c都是手动来分配释放 释放 计数内存,其过程非常复杂. ARC技术推出后,貌似世界和平了很多,但是其实ARC并不等同于Java或者C#中的垃圾回收,AR ...

  9. yii---定义全局函数

    YII它不像Thinkphp等框架一样,已经有全局函数,YII要使用全局函数需要自己去定义,然后在入口文件中进行引入: 例如:我们看 yii 的入口文件: 看到这里,我们看到有个 autoload.p ...

  10. 如何搭建SoC项目的基本Testbench【zz】

    原文地址:http://bbs.eetop.cn/thread-442797-1-8.html 写这个文档的目的是让大家对搭建SoC项目的Testbench有一个比较清晰的认识,可以根据这个文档来一步 ...