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. Internet Message Access Protocol --- IMAP协议

    https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol   Internet Message Access Protocol

  2. openvpn 负载均衡方案

    这些方案的前提是,vpnserver的key都是一样的.方案1在openvpn客户端设两个配置文件,我们自己手动去连接去选择方案2在openvpn 的配置文件里面加个随机参数remote 8.8.8. ...

  3. MySQL对数据表进行分组查询(GROUP BY)

    MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  4. Apache Shiro 反序列化RCE漏洞

    漏洞介绍 漏洞类型 :JAVA反序列化(RCE) 影响版本 :Apache Shiro 1.2.4及其之前版本 漏洞评级 :高危 漏洞分析 #: 下载漏洞环境: git clone https://g ...

  5. linux shell中curl 发送post请求json格式问题

    今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...

  6. vim ctrl+v垂直选取产生 e353错误

    原因是/etc/vim/vimrc中map ctrl+v为其他作用了 解决方法: 删除掉map<C-V>...项即可

  7. 彻底关闭window10 专业版 企业版 windows defender

    按照上面图中的,关闭windows defender 设置为已启用,这样就可以彻底关闭 windows defender了

  8. numpy协方差矩阵numpy.cov

    numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)[source] Estim ...

  9. ubuntu部署nginx

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade再判断系统是否内置了add-apt-repository命令,如果没有执行下列命令安装 sudo ...

  10. Jenkins构建报错(Jenkins is reserved for jobs with matching label expression)解决办法

    Jenkins构建报错Jenkins is reserved for jobs with matching label expression 原因节点配置导致 修改配置