打开hot100
128. 最长连续序列 - 力扣(LeetCode)
class Solution {
public:int longestConsecutive(vector<int>& nums) {unordered_set<int> ust(nums.begin(), nums.end());int len = 0;for(auto x : nums){if(ust.contains(x -1)){continue;} int y = x + 1;while(ust.contains(y)){y++;} len = max(len, y - x);if(len * 2 > ust.size()) break;}return len;}
};
2. 两数相加 - 力扣(LeetCode)
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* dummy = new ListNode();ListNode* cur = dummy;int carry = 0; // 进位while(l1 || l2 || carry){if(l1){carry += l1->val;l1 = l1->next;}if(l2){carry += l2->val;l2 = l2->next;}cur->next = new ListNode(carry % 10);carry /= 10;cur = cur->next;}return dummy->next;}
};
79. 单词搜索 - 力扣(LeetCode)
class Solution {public:bool exist(vector<vector<char>>& board, string word) {int m = board.size();int n = board[0].size();for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(dfs(board, word, 0, i, j)) return true;}}return false;}
private:bool dfs(vector<vector<char>>& board, string & word, int idx, int i, int j){int m = board.size();int n = board[0].size();if(i < 0 || i >= m || j < 0 || j >= n) return false;if(board[i][j] != word[idx]) return false;if(idx == word.size() - 1) return true;char saved = board[i][j];board[i][j] = '#';bool found = dfs(board, word, idx + 1, i + 1, j) ||dfs(board, word, idx + 1, i - 1, j) ||dfs(board, word, idx + 1, i, j + 1) ||dfs(board, word, idx + 1, i, j - 1);board[i][j] = saved;return found;}
};