标签:margin -- dice name index rate one key ble
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.
版本1:O(n^2) 【暴力 原始版本】O(1)
classSolution(object):def twoSum(self, nums, target): length = len(nums)for i in range(length):for j in range(i+1,length)://游标后移if nums[i]+ nums[j]== target:return[i,j]classSolution(object):def twoSum(self, nums, target):for index , item in enumerate(nums):for past_index , past_item in enumerate(nums[index+1:],index+1):if item + past_item == target:return[index, past_index]classSolution(object):def twoSum(self, nums, target): dd = dict()for index , value in enumerate(nums): dd[value]= indexfor index , value in enumerate(nums): tt = target - valueif dd.has_key(tt)and dd[tt]!= index:return[index , dd[tt]]
classSolution(object):def twoSum(self, nums, target): dd = dict()for index , value in enumerate(nums): tt = target - valueif dd.has_key(tt)and dd[tt]!= index:return[dd[tt],index]else: dd[value]= indexpublic int[] twoSum(int[] nums, int target){Map<Integer,Integer> map = new HashMap<Integer,Integer>();for( int i=0;i<nums.length;i++){if(!map.containsKey(target - nums[i])){ map.put(nums[i], i );}else{ int[] rs ={ map.get(target-nums[i]), i };return rs;}}return nums;}标签:margin -- dice name index rate one key ble
原文地址:http://www.cnblogs.com/flyfatty/p/6624787.html