日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

哈希表-map(对于python来说是字典)

發布時間:2024/4/13 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 哈希表-map(对于python来说是字典) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 兩數之和

給定一個整數數組 nums 和一個整數目標值 target,請你在該數組中找出 和為目標值 target 的那 兩個 整數,并返回它們的數組下標。

你可以假設每種輸入只會對應一個答案。但是,數組中同一個元素在答案里不能重復出現。

你可以按任意順序返回答案。

示例 1:

輸入:nums = [2,7,11,15], target = 9 輸出:[0,1] 解釋:因為 nums[0] + nums[1] ==
9 ,返回 [0, 1] 。
示例 2:

輸入:nums = [3,2,4], target = 6 輸出:[1,2]
示例 3:

輸入:nums = [3,3], target = 6 輸出:[0,1]

class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}for i,n in enumerate(nums):res = target - nif res in d:return[d[res],i]# d存每個數字的下標else:d[n] = i

15. 三數之和

給你一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?請你找出所有和為 0 且不重復的三元組。

注意:答案中不可以包含重復的三元組。

作者:wu_yan_zu
鏈接:https://leetcode-cn.com/problems/3sum/solution/pai-xu-shuang-zhi-zhen-zhu-xing-jie-shi-python3-by/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

雙指針解法是一層for循環num[i]為確定值,然后循環內有left和right下標作為雙指針,找到nums[i] + nums[left] + nums[right] == 0。

示例 1:

輸入:nums = [-1,0,1,2,-1,-4] 輸出:[[-1,-1,2],[-1,0,1]]
示例 2:

輸入:nums = [] 輸出:[]
示例 3:

輸入:nums = [0] 輸出:[]

class Solution:def threeSum(self, nums):ans = []n = len(nums)nums.sort()for i in range(n):left = i + 1right = n - 1# 以為已經排序過了 如果某元素大于0 則之后的和不可能為0if nums[i] > 0:break'''錯誤的去重 nums[i] == nums[i+1] 會漏掉 -1 -1 2這種'''# 去重 去掉重復的數字 比如 -4 -1 -1 -1 2 需要去掉重復的第一個數字的-1# 因為nums[i]是第一個數 之后的i不能與之前的i-1數相同 否則會重復計算相同的數組if i >= 1 and nums[i] == nums[i - 1]:continuewhile left < right:total = nums[i] + nums[left] + nums[right]# 說明右邊的數大了if total > 0:right -= 1# 說明左邊的數大了elif total < 0:left += 1else:ans.append([nums[i], nums[left], nums[right]])# 去重邏輯 應該放在找到一個三元組之后 去重是去 與中間數或最后一個數重復的數while (left < right and nums[right] == nums[right - 1]): right -= 1while (left < right and nums[left] == nums[left + 1]): left += 1# 找到答案后 雙指針同時收縮right -= 1left += 1return ans

454. 四數相加 II

給你四個整數數組 nums1、nums2、nums3 和 nums4 ,數組長度都是 n ,請你計算有多少個元組 (i, j, k, l) 能滿足:

0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

示例 1:

輸入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] 輸出:2
解釋: 兩個元組如下:

  • (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
  • (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
    示例 2:
  • 輸入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] 輸出:1

    class Solution(object):def fourSumCount(self, nums1, nums2, nums3, nums4):""":type nums1: List[int]:type nums2: List[int]:type nums3: List[int]:type nums4: List[int]:rtype: int"""res = {}for n1 in nums1:for n2 in nums2:key = n1+n2if key not in res:res[key]=1else:res[key]+=1# 如果res中存在 -(n3+n4)=n1+n2count = 0for n3 in nums3:for n4 in nums4:key = -n3-n4if key in res:count+=res[key] return count

    18. 四數之和

    給你一個由 n 個整數組成的數組 nums ,和一個目標值 target 。請你找出并返回滿足下述全部條件且不重復的四元組 [nums[a], nums[b], nums[c], nums[d]] (若兩個四元組元素一一對應,則認為兩個四元組重復):

    0 <= a, b, c, d < n
    a、b、c 和 d 互不相同
    nums[a] + nums[b] + nums[c] + nums[d] == target
    你可以按 任意順序 返回答案 。

    示例 1:

    輸入:nums = [1,0,-1,0,-2,2], target = 0
    輸出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
    示例 2:

    輸入:nums = [2,2,2,2,2], target = 8 輸出:[[2,2,2,2]]

    思路:
    四數之和的雙指針解法是兩層for循環nums[k] + nums[i]為確定值,依然是循環內有left和right下標作為雙指針,找出nums[k] + nums[i] + nums[left] + nums[right] == target的情況,三數之和的時間復雜度是O(n2)O(n^2)O(n2),四數之和的時間復雜度是O(n3)O(n^3)O(n3)

    class Solution(object):def fourSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[List[int]]"""nums.sort()n =len(nums)# if n < 4:# return []res = []for i in range(n):if i>0 and nums[i] == nums[i-1]:continuefor j in range(i+1,n):if j>i+1 and nums[j] == nums[j-1]:continueleft = j+1right = n-1while left<right:total=nums[i]+nums[j]+nums[left]+nums[right]if total >target:right -=1elif total <target:left += 1else:res.append([nums[i],nums[j],nums[left],nums[right]])while left < right and nums[right] == nums[right-1]:right -= 1while left < right and nums[left] == nums[left+1]:left += 1left += 1right -= 1return res

    總結

    以上是生活随笔為你收集整理的哈希表-map(对于python来说是字典)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。