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 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...
随机推荐
- 对Spark的理解
Spark作为一个新的分布式计算引擎正慢慢流行起来,越来越来的企业也准备用它的替换MapReduce,根据自己在工作的一些体会谈谈的优势. 分布式计算归根到底还是一个Map和Reduce操作,Map操 ...
- 分享常用的GoLang包工具
包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/eclipse/paho.mqt ...
- 防恶意解析,禁止用IP访问网站的Apache设置 修改 httpd.conf 实现
一般来说,网站可以用域名和IP来访问.你的网站可以通过IP直接访问,本来这没什么问题,但是会有些隐患: 由于搜索引擎也会收录你的IP地址的页面,所以同一个页面搜索引擎会重复收录,造成页面的权重不如单个 ...
- 多线程学习-基础(六)分析wait()-notify()-notifyAll()
一.理解wait()-notify()-notifyAll()obj.wait()与obj.notify()必须要与synchronized(Obj)一起使用,也就是wait,notify是针对已经获 ...
- mysql 远程登录与表名大小写问题
好久没写博客了,这段时间在学习一个开源的项目,里面使用到了mysql,好久没使用mysql了.在使用过程中遇到了一个问题,远程登陆.报错信息很明显,连接失败.解决思路如下: 1. 首先检查到服务器网络 ...
- [SinGuLaRiTy] 数论题目复习
[SinGuLaRiTy-1020] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 1464] Hankson 题目描述 H ...
- Linux串口参数设置
linux串口编程参数配置详解 1.linux串口编程需要的头文件 #include <stdio.h> //标准输入输出定义#include <stdlib.h&g ...
- oracle 集群jndi部署方式
一般部署oracle jndi的方式: ...jdbc.url=jdbc:oracle:thin:@10.196.20.xx:1521:SID ... 集群部署方式 : ... jdbc.url= ...
- AtCoder Beginner Contest 120 题解
题目链接:https://atcoder.jp/contests/abc120 A Favorite Sound 分析:答案为. 代码: #include <iostream> using ...
- the swap trick用于锐减过剩容量
1.由于vector的复制构造函数只为被复制的vector分配它所需要的空间,故可以用如下的方式来削减vector v中过剩的容量:vector<int>(v).swap(v) 2.the ...