001-Two Sum
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.
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的更多相关文章
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- 【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 ...
- No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- LeetCode--No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- [Leetcode][001] Two Sum (Java)
题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...
- 001 Two Sum 两个数的和为目标数字
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 【LeetCode】001. Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode 【1】 Two Sum --001
5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ...
- 《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 ...
随机推荐
- goldengate 过滤对某张表的复制操作
在复制进程中配置下面的参数可以实现对一个用户下的某些表进行过滤,在复制的时候 不做任何操作. MAPEXCLUDE: Valid for Replicat Use the MAPEXCLUDE par ...
- 题目1458:汉诺塔III(不一样的汉诺塔递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1458 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- Android服务开发——WebService
我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...
- C#取得Web程序和非Web程序的根目录的N种取法
取得控制台应用程序的根目录方法方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.BaseDi ...
- WEB前端开发规范文档[转]
为新项目写的一份规范文档, 分享给大家. 我想前端开发过程中, 无论是团队开发, 还是单兵做站, 有一份开发文档做规范, 对开发工作都是很有益的. 本文档由本人编写, 部分意见来源于网络, 以此感谢, ...
- 转利用python实现电影推荐
“协同过滤”是推荐系统中的常用技术,按照分析维度的不同可实现“基于用户”和“基于产品”的推荐. 以下是利用python实现电影推荐的具体方法,其中数据集源于<集体编程智慧>一书,后续的编程 ...
- 设置ubuntu默认中文字符
一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8,这个可以在 /etc/environment中看到:sudo gedit /etc/environment ...
- 关于jquery的css的一些知识
Query实例CSS 样式表动态选择本实例主要说的还是jquery的选择器,关于jquery的css的一些知识用类似 $("li").css("cursor", ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 洞察行业领先者的前沿思想——第五届TOP100全球软件案例研究峰会精彩谢幕
(第五届TOP100summit开幕式现场) 12月09日-12日,由msup主办的第五届TOP100全球软件案例研究峰会(以下简称TOP100summit)在北京国家会议中心举行,作为互联网行业最有 ...