Two Sum Leetcode Python Java
来源:互联网 发布:什么是淘宝软文 编辑:程序博客网 时间:2024/05/17 09:09
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
Python:
#coding:utf-8class Solution: def twoSum(self,list1,target): if len(list1)<0: return false end=[] subdict={} for i in range(len(list1)): if list1[i] in subdict: end.append([subdict[list1[i]],i]) else: subdict[target-list1[i]]=i return endif __name__=="__main__": list1=[1,2,3,4] target=5 print Solution().twoSum(list1,target)
Java:
java: public int[] twoSum(int[],numbers,int target){ int[] result=new int[2]; Map<Integer,Integer> map=new HashMap<Integer,Integer>(); for(int i=0;i<numbers.length;i++){ if(map.containsKey(target-numbers[i])){ reult[0]=map.get(target-numbers[i]); result[1]=i; } else{ map.put(numbers[i]); } } return result; }
0 0
- Two Sum Leetcode Python Java
- Add Two Sum Leetcode Python java
- 【Leetcode】【python】Two Sum, Two Sum II
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- 【Leetcode】two sum JAVA
- LeetCode java Two Sum
- Two Sum leetcode Java
- [LeetCode][Java] Two Sum
- leetcode:Two Sum 【Java】
- LeetCode|Two Sum-java
- leetcode Two Sum(Java)
- leetcode two sum python 实现
- 【LeetCode with Python】 Two Sum
- 1.Two Sum Leetcode Python
- LeetCode : No1 Two Sum Python
- [leetcode]-[Two Sum]-python实现
- [Leetcode.python] 001. Two Sum
- Consul入门06 - 键/值对数据
- Problem-D
- HTML5 canvas画布(五)
- 001-整数快速幂-归纳法-《算法设计技巧与分析》M.H.A学习笔记
- POJ1273 Drainage Ditches 【最大流】
- Two Sum Leetcode Python Java
- 10年乔布斯对flash的思考迎来了web3.0时代(html5的应用)
- centos系统,基于nginx服务器,用https访问php页面总弹出下载页面问题完美解决
- Android中listView的下拉加载功能实现
- Ubuntu环境下配置J2EE开发环境jdk+tomcat+mysql+myeclipse
- 274. H-Index
- 2096小明A+B
- 固定电话的正则表达式
- oracle快速方便复制某一张表的表字段