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. Delete触发器

  2. sencha touch 模仿tabpanel导航栏TabBar(2013-11-7)

    基于sencha touch 2.2所写 代码: /* *模仿tabpanel导航栏 */ Ext.define('ux.TabBar', { alternateClassName: 'tabBar' ...

  3. CF 1073C Vasya and Robot(二分答案)

    C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. sonarqube插件开发(三) 调试插件

    环境 windows + eclipse +sonarqube server 5.6.4 准备 新建一个maven项目,其中加入了一些插件所用的jar <project xmlns=" ...

  5. vue过滤动画

    一.使用<transition name="fade"></transition>标签 name="fade", 是创建个fade的类名 ...

  6. 记录一下gitlab通过CAS登录慢的问题

    测试反应说gitlab通过CAS登录比较慢,第一次登录的时候需要大概30秒才能登录进去 gitlab的日志中有处理每一个请求所用的时间,看了一下日志,每个有记录的请求都是在50毫秒内返回的,所以应该不 ...

  7. C语言位操作--奇偶校验算法

    信息是以比特流的方式传输的,类似01000001.在传输过程中,有可能会发生错误,比如,我们存储了01000001,但是取出来却是01000000,即低位由0变成了1.为了检测到这种错误,我们可以通过 ...

  8. python基础-动态加载lazy_import(利用__import__)

    看了一天动态加载,普遍有这么几种方法,总结一下,由简入深,本文仅对查到的栗子们做个引用……省去你们大把查资料的时间= = 主要思想:把模块(文件)名.类名.方法名当成了变量 然后利用__import_ ...

  9. Timer应用之Interval优化

    开发中, 有时有这种场景,使用 Timer 的 Timer_Elapsed 间隔  执行(如:从数据库)获取数据 与 现有 应用服务器中的 静态变量数据(起到缓存的目的)做 对比 ,若有改变,则 更新 ...

  10. hihocoder 1331 - 扩展二进制数 - [hiho一下168周]

    题目链接:http://hihocoder.com/problemset/problem/1331 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们都知道二进制数的每 ...