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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode(20210412-20210418 第一周 每日一题)

發(fā)布時間:2025/4/5 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode(20210412-20210418 第一周 每日一题) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、兩數(shù)之和(20210412)給定一個整數(shù)數(shù)組 nums 和一個整數(shù)目標值 target,請你在該數(shù)組中找出 和為目標值 的那 兩個 整數(shù),并返回它們的數(shù)組下標。 你可以假設每種輸入只會對應一個答案。但是,數(shù)組中同一個元素在答案里不能重復出現(xiàn)。 你可以按任意順序返回答案。示例 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]提示: * 2 <= nums.length <= 103 * -109 <= nums[i] <= 109 * -109 <= target <= 109 * 只會存在一個有效答案class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:# for i in range(len(nums)):# diff = target - nums[i]# if diff in set(nums[i+1:]):# return [i, i + nums[i+1:].index(diff) + 1]dict_num = {}for i,n in enumerate(nums):diff = target - nums[i]if diff in dict_num:return [dict_num[diff], i]else:dict_num[n] = I2、反轉鏈表-206反轉一個單鏈表。 示例: 輸入: 1->2->3->4->5->NULL 輸出: 5->4->3->2->1->NULLclass Solution:def reverseList(self, head: ListNode) -> ListNode:pre,cur = None,headwhile cur:temp = cur.nextcur.next = prepre = curcur = tempreturn pre3. 兩數(shù)相加(2)給你兩個 非空 的鏈表,表示兩個非負的整數(shù)。它們每位數(shù)字都是按照 逆序 的方式存儲的,并且每個節(jié)點只能存儲 一位 數(shù)字。 請你將兩個數(shù)相加,并以相同形式返回一個表示和的鏈表。 你可以假設除了數(shù)字 0 之外,這兩個數(shù)都不會以 0 開頭。示例 1:輸入:l1 = [2,4,3], l2 = [5,6,4] 輸出:[7,0,8] 解釋:342 + 465 = 807.示例 2: 輸入:l1 = [0], l2 = [0] 輸出:[0]示例 3: 輸入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] 輸出:[8,9,9,9,0,0,0,1]提示: * 每個鏈表中的節(jié)點數(shù)在范圍 [1, 100] 內(nèi) * 0 <= Node.val <= 9 * 題目數(shù)據(jù)保證列表表示的數(shù)字不含前導零class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:head = ListNode(l1.val + l2.val)cur = headwhile l1.next or l2.next:l1 = l1.next if l1.next else ListNode()l2 = l2.next if l2.next else ListNode()cur.next = ListNode(l1.val + l2.val + cur.val//10)cur.val = cur.val % 10cur = cur.nextif cur.val >= 10:cur.next = ListNode(cur.val//10)cur.val = cur.val % 10return head4. 無重復字符的最長子串(3)給定一個字符串,請你找出其中不含有重復字符的 最長子串 的長度。示例 1: 輸入: s = "abcabcbb" 輸出: 3 解釋: 因為無重復字符的最長子串是 "abc",所以其長度為 3。示例 2: 輸入: s = "bbbbb" 輸出: 1 解釋: 因為無重復字符的最長子串是 "b",所以其長度為 1。示例 3: 輸入: s = "pwwkew" 輸出: 3 解釋: 因為無重復字符的最長子串是 "wke",所以其長度為 3。請注意,你的答案必須是 子串 的長度,"pwke" 是一個子序列,不是子串。示例 4: 輸入: s = "" 輸出: 0提示: * 0 <= s.length <= 5 * 104 * s 由英文字母、數(shù)字、符號和空格組成class Solution:def lengthOfLongestSubstring(self, s: str) -> int:hash_map = {}max_len = 0left = 0for i,c in enumerate(s):if c in hash_map:left = max(left, hash_map[c] + 1)hash_map[c] = imax_len = max(max_len, i - left + 1)return max_len5、有效的括號(20)給定一個只包括 '(',')','{','}','[',']' 的字符串 s ,判斷字符串是否有效。 有效字符串需滿足: 1. 左括號必須用相同類型的右括號閉合。 2. 左括號必須以正確的順序閉合。示例 1: 輸入:s = "()" 輸出:true示例 2: 輸入:s = "()[]{}" 輸出:true示例 3: 輸入:s = "(]" 輸出:false示例 4: 輸入:s = "([)]" 輸出:false示例 5: 輸入:s = "{[]}" 輸出:trueclass Solution:def isValid(self, s: str) -> bool:dict_ = {")":"(", "}":"{", "]":"["}stack = []for item in s:if stack and item in dict_:if stack[-1] == dict_[item]:stack.pop()else:return Falseelse:stack.append(item)return not stackclass Solution:def isValid(self, s: str) -> bool:while "{}" in s or "()" in s or "[]" in s:s = s.replace("{}","")s = s.replace("()","")s = s.replace("[]","")return s == ""

?

總結

以上是生活随笔為你收集整理的Leetcode(20210412-20210418 第一周 每日一题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。