124. 二叉树中的最大路径和
困难题,我先跳过。
128. 最长连续序列
不会。
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> hash = new HashSet<Integer>();
for(int x : nums) hash.add(x); //放入hash表中
int res = 0;
for(int x : hash)
{
if(!hash.contains(x-1))
{
int y = x; //以当前数x向后枚举
while(hash.contains(y + 1)) y++;
res = Math.max(res, y - x + 1); //更新答案
}
}
return res;
}
}
二刷,依然不会。
代码是看懂了:
python:
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
longest_streak = 0
num_set = set(nums)
for num in num_set:
if num - 1 not in num_set:
current_num = num
current_streak = 1
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
longest_streak = max(longest_streak, current_streak)
return longest_streak
cpp:
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> num_set;
for (const int& num : nums) {
num_set.insert(num);
}
int longestStreak = 0;
for (const int& num : num_set) {
if (!num_set.count(num - 1)) {
int currentNum = num;
int currentStreak = 1;
while (num_set.count(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = max(longestStreak, currentStreak);
}
}
return longestStreak;
}
};
下面是我写的python代码:
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
longest = 0
nums_set = set(nums)
for num in nums_set:
if num - 1 not in nums_set:
cur_num = num
cur = 1
while cur_num + 1 in nums_set:
cur_num += 1
cur += 1
longest = max(longest, cur)
return longest
136. 只出现一次的数字
我想到的简单的解法:
class Solution:
def singleNumber(self, nums: List[int]):
if not nums or len(nums) == 0:
return 0
dic = {}
for i in range(len(nums)):
if nums[i] in dic:
dic[nums[i]] += 1
else:
dic[nums[i]] = 0
for k, v in dic.items():
if v == 0:
return k
答案:
我写的python代码:
class Solution:
def singleNumber(self, nums: List[int]):
res = nums[0]
for i in range(1, len(nums)):
res ^= nums[i]
return res
c++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ret = 0;
for (auto e: nums) ret ^= e;
return ret;
}
};
c++果然快不少。
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(lambda x, y: x ^ y, nums)
139. 单词拆分
想到要用动态规划,但是不会写。
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
n=len(s)
dp=[False]*(n+1)
dp[0]=True
for i in range(n):
for j in range(i+1,n+1):
if(dp[i] and (s[i:j] in wordDict)):
dp[j]=True
return dp[-1]
还得来复习。
二刷,还是看了答案。
看着答案写的:
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
for j in range(i + 1, n + 1):
if dp[i] and (s[i: j] in wordDict):
dp[j] = True
return dp[-1]
141. 环形链表
我记得这道题要用快慢指针做。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head):
if not head or not head.next:
return False
slow = fast = head
while fast:
if not fast.next:
return False
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
二刷:
python:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head):
if not head or not head.next:
return False
slow = fast = head
while fast:
if not fast.next:
return False
slow = slow.next
fast = fast.next.next
if slow == fast: return True
return False
c++:
class Solution {
public:
bool hasCycle(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return false;
}
ListNode* slow = head;
ListNode* fast = head->next;
while (slow != fast) {
if (fast == nullptr || fast->next == nullptr) {
return false;
}
slow = slow->next;
fast = fast->next->next;
}
return true;
}
};