Leetcode(20210412-20210418 第一周 每日一题)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode(20210412-20210418 第一周 每日一题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、兩數之和(20210412)給定一個整數數組 nums 和一個整數目標值 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]提示:
* 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. 兩數相加(2)給你兩個 非空 的鏈表,表示兩個非負的整數。它們每位數字都是按照 逆序 的方式存儲的,并且每個節點只能存儲 一位 數字。
請你將兩個數相加,并以相同形式返回一個表示和的鏈表。
你可以假設除了數字 0 之外,這兩個數都不會以 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]提示:
* 每個鏈表中的節點數在范圍 [1, 100] 內
* 0 <= Node.val <= 9
* 題目數據保證列表表示的數字不含前導零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 由英文字母、數字、符號和空格組成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 第一周 每日一题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异常检测-孤立森林(IsolationF
- 下一篇: Leetcode(20210419-20