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].
详见:https://leetcode.com/problems/two-sum/description/
给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。
要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。
Java实现:
暴力解:
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
for(int i=0;i<nums.length;++i){
for(int j=i+1;j<nums.length;++j){
if(nums[i]+nums[j]==target){
res[0]=i;
res[1]=j;
}
}
}
return res;
}
}
Map:空间换时间
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res=new int[2];
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;++i){
if(map.containsKey(target-nums[i])){
res[0]=map.get(target-nums[i]);
res[1]=i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}
python:
方法一:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hash={}
for i in range(len(nums)):
if target-nums[i] in hash:
return hash[target-nums[i]],i
else:
hash[nums[i]]=i
return -1,-1
方法二:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
if target-nums[i] in nums and nums.index(target-nums[i])!=i:
return nums.index(target-nums[i]),i
return -1,-1
golang:
方法一:
func twoSum(nums []int, target int) []int {
for i:=0;i<len(nums);i++{
for j:=i+1;j<len(nums);j++{
if nums[i]+nums[j]==target{
return []int{i,j}
}
}
}
return []int{}
}
方法二:
func twoSum(nums []int, target int) []int {
hash:=make(map[int]int)
for i,v:=range nums{
j,ok:=hash[target-v]
if ok{
return []int{i,j}
}
hash[v]=i
}
return []int{}
}
001 Two Sum 两个数的和为目标数字的更多相关文章
- 015 3Sum 三个数的和为目标数字
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- js 数组里面任意两个数的和与目标值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 2016网易实习生编程题:数组中两个数的和等于sum
题目 找出数组中两个数的和等于sum的这两个数 解题 这个题目做过很多次了,利用HashMap,key为 sum-A[i] value为 i 当 加入HashMap时候A[i] 已经存在map中,ge ...
- 牛客网2016.4.11(两个数相加为sum/计数一个int型的二进制有多少个1/二叉树是否左右对称)
求最小的两个数相加为sum //求最小的两个数相加为sum public ArrayList<Integer> FindNumbersWithSum(int [] array,int su ...
- shell实现两个数的相加
刚开始的时候写,一直写不对:看似简单的功能,但是一定要小心:函数的定义: funciton functionName {.....}在functionName和{之间一定有空格啊! 我就是没加空格,就 ...
- Hard 不用+号实现两个数之和 @CareerCup
例子: 759+674 1)不考虑进位: 323 2)只考虑进位:1110 3)两者之和:1433 递归求解c package Hard; /** * Write a function that ...
- 【剑指offer】和为定值的两个数
转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...
- 【剑指offer学习】求和为定值的两个数(拓展)
接着上面一篇文章: http://blog.csdn.net/u013476464/article/details/40651451 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...
随机推荐
- 网易CentOS yum源
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
- 2019-RHCE-红帽题库(稳定)
rhce7 考题2台服务器设置yum源[aa]name=aabaesurl=ftp://server.rhce.cc/dvdenabled=1gpgcheck=0 cd /etc/yum.repos. ...
- History命令用法15例
以下内容为转载: 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTT ...
- kuangbin专题16B(kmp模板)
题目链接: https://vjudge.net/contest/70325#problem/B 题意: 输出模式串在主串中出现的次数 思路: kmp模板 在 kmp 函数中匹配成功计数加一, 再令 ...
- 华为敏捷/DevOps实践:如何开好站立会议
大家好,我是华为云的产品经理 恒少: 作为布道师和产品经理,出差各地接触客户是常态,经常和华为云的客户交流.布道.技术沙龙,但是线下交流,覆盖的用户总还是少数. 我希望可以借线上的平台,和用户持续交流 ...
- [ZJOI2009]假期的宿舍 BZOJ 1433 二分图匹配
题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C 与 A 不认识. ...
- angularJs增加行的操作
<!-- 编辑窗口 --> <div class="modal fade" id="editModal" tabindex="-1& ...
- react 中文文档案例七 (温度计)
const scaleNames = { c: 'Celsius', f: 'Fahrenheit' }; function toCelsius(fahrenheit) { ) * / ; } fun ...
- BestCoder Round #66 1002
GTW likes gt Accepts: 75 Submissions: 261 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...
- redis实现SSO单点登录,集群,分布式锁
https://blog.csdn.net/aussme/article/details/80660443