GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/015_3Sum.py at master · robotmechanic/leetcode Expand file tree/ Copy path015_3Sum.pyMore file actions 76 lines (74 loc) · 2.41 KB / Copy path015_3Sum.pyFile metadata and controls76 lines (74 loc) · 2.41 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 class Solution(object): # def threeSum(self, nums): # # skip duplicate # # O(n^3) # if len(nums) < 3: # return [] # nums.sort() # ls = len(nums) # result = [] # for i in range(0, ls - 2): # if nums[i] > 0: # break # if i > 0 and nums[i] == nums[i - 1]: # continue # j = i + 1 # k = ls - 1 # while(j < k): # temp = [nums[i], nums[j], nums[k]] # s = sum(temp) # jtemp = nums[j] # ktemp = nums[k] # if s <= 0: # j += 1 # while(j < k and jtemp == nums[j]): # j += 1 # if s == 0: # result.append(temp) # else: # k -= 1 # while(j < k and ktemp == nums[k]): # k -= 1 # return result # def threeSum(self, nums): # """ # :type nums: List[int] # :rtype: List[List[int]] # """ # # using multiple 2 sum # nums.sort() # result, visited = set(), {} # for i in xrange(len(nums) - 2): # table, target = {}, -nums[i] # if nums[i] not in visited: # for j in xrange(i + 1, len(nums)): # if nums[j] not in table: # table[target - nums[j]] = j # else: # result.add((nums[i], target - nums[j], nums[j])) # visited[nums[i]] = 1 # return list(result) def threeSum(self, nums): res = [] nums.sort() ls = len(nums) for i in range(ls - 2): if i > 0 and nums[i] == nums[i - 1]: continue j = i + 1 k = ls - 1 while j < k: curr = nums[i] + nums[j] + nums[k] if curr == 0: res.append([nums[i], nums[j], nums[k]]) while j < k and nums[j] == nums[j + 1]: j += 1 while j < k and nums[k] == nums[k - 1]: k -= 1 j += 1 k -= 1 elif curr < 0: j += 1 else: k -= 1 return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |