[Letcode] 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
给定一个integer类型的数组,让你返回数组中两个元素相加起来等于给定的值得元素的索引数组
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.
返回数组的索引已置为0
以下是我的解决方案:
int[] array = new int[];
for (int i = ; i < nums.Length - ; i++)
{
int num1 = nums[i];
int j = i + ; while(j < nums.Length) {
int num2 = nums[j];
int result = num1 + num2; if (result.CompareTo(target) == )
{
array[] = i;
array[] = j;
i = j = nums.Length + ;
} j++;
}
} return array;
[Letcode] 1. Two Sum的更多相关文章
- LeetCode - Two Sum
		
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
 - Leetcode 笔记 113 - Path Sum II
		
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
 - Leetcode 笔记 112 - Path Sum
		
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
 - POJ 2739. Sum of Consecutive Prime Numbers
		
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
 - BZOJ 3944 Sum
		
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
 - [LeetCode] Path Sum III 二叉树的路径和之三
		
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
 - [LeetCode] Partition Equal Subset Sum 相同子集和分割
		
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
 - [LeetCode] Split Array Largest Sum 分割数组的最大值
		
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
 - [LeetCode] Sum of Left Leaves 左子叶之和
		
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
 
随机推荐
- Could not find action or result
			
[WARN ] 2013-11-21 14:08:16 :Could not find action or resultThere is no Action mapped for namespace ...
 - Leetcode006 ZigZag Conversion
			
/* simple simulation algorithm * we cann`t make sure the size of the string, * so it had better to s ...
 - md5的一些用法
			
package md5; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * ...
 - golang初体验
			
使用golang开发已经有一个多月了,除了一开始遇到的各种小白问题,IDE的选择,gopath,goroot的配置...后边还算顺风顺水的.实践体验,golang真的适合快速开发.特别是后台开发,直接 ...
 - 10 Code Coverage Tools for C & C++
			
Code coverage is a measure used in software testing that describes the degree to which the source co ...
 - angular service讲解
			
controller是相对独立的,也就是说,两个controller之间,内存是不共享的,这个controller是无法访问其他其他controller的属性或者方法的; 以前,我都是通过localS ...
 - SpringData JPA 排除 扫描  exclude-filter 不能使用解决
			
在加上JPA的配置外,还需要再加上 : xmlns:repository="http://www.springframework.org/schema/data/repository&quo ...
 - 调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍
			
代码难免会有Bug,每次我们在Chrome调试代码时,总是会进入各种各样的库代码(比如jQuery.Zepto),但实际上很多时候我们并不希望这样,要是能把这些库代码“拉黑”多好啊. 广大码农喜闻乐见 ...
 - fstab
			
开机后,挂载windows盘失败, 进入了一个检测shell,该shell下是以只读方式挂载根分区的,要使fstab可写,要执行mount -o remount,rw / /etc/fstab文件的解 ...
 - THINKPHP 清除HTML注释、换行符、空格、制表符等
			
thinkphp3.2 3.2中取消了配置文件中的 'TMPL_STRIP_SPACE' 属性,所以我们先来修改:\ThinkPHP\Library\Think\Template.class.php ...