每日一题06

2023年04月08日 · 0 分钟 · 0 字 · ZhaoYang

每日一题05

每日一题 哈希表 python中defaultdict类的使用: https://blog.nowcoder.net/n/2aef535877a84165b8b7d99032ecbf09 python中Counter类的使用: https://blog.csdn.net/sinat_28576553/article/details/99131954

2023年04月07日 · 1 分钟 · 51 字 · ZhaoYang

每日一题04

每日一题 链表定义 cpp struct ListNode{ int val; ListNode *next; ListNode(int x) : val(x),next(NULL) {} } python class ListNode: def __init__(self, val, next = None): self.val = val self.next = next 移除链表元素 https://leetcode.cn/problems/remove-linked-list-elements/ cpp /** * 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* removeElements(ListNode* head, int val) { while(head != NULL && head->val == val){ ListNode* temp = head; head = head-> next; delete temp; } ListNode* cur = head; while(cur != NULL && cur->next != NULL){ if(cur->next->val == val){ ListNode* temp = cur->next; cur->next = cur->next->next; delete temp; }else{ cur = cur->next; } } return head; } }; python ...

2023年04月05日 · 1 分钟 · 225 字 · ZhaoYang

每日一题03

每日一题 滑动窗口 leetcode 209 长度最小的子数组 https://leetcode.cn/problems/minimum-size-subarray-sum/ 代码: cpp class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int sum = 0; int j = 0; int res = nums.size() + 10; for(int i = 0; i < nums.size(); i++ ){ sum += nums[i]; while(sum >= target ){ res = min(res, i - j + 1); sum -= nums[j++]; } } res = (res == nums.size() + 10 ? 0 : res); return res; } }; python: class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: res = len(nums) + 10 sum = 0 j = 0 for i in range(len(nums)): sum += nums[i] while sum >= target: res = min(res, i - j + 1) sum -= nums[j] j += 1 if res == len(nums) + 10: res = 0 return res leetcode 904 水果成篮 https://leetcode.cn/problems/fruit-into-baskets/ 代码: cpp: class Solution { public: int totalFruit(vector<int>& fruits) { int res = 0; unordered_map<int, int> cnt; int j = 0; for(int i = 0; i < fruits.size(); i++){ cnt[fruits[i]]++; while(cnt.size() > 2){ auto it = cnt.find(fruits[j]); it->second--; if(it->second == 0) cnt.erase(fruits[j]); j++; } res = max(res, i - j + 1); } return res; } }; python ...

2023年04月04日 · 2 分钟 · 930 字 · ZhaoYang

每日一题02

每日一题 双指针算法 leetcode 27 移出元素 https://leetcode.cn/problems/remove-element/ 代码: cpp: class Solution { public: int removeElement(vector<int>& nums, int val) { int fast = 0; int slow = 0; for (fast = 0; fast < nums.size(); fast++){ if(nums[fast] != val){ nums[slow++] = nums[fast]; } } return slow; } }; python: class Solution: def removeElement(self, nums: List[int], val: int) -> int: slow, fast = 0, 0 for fast in range(len(nums)): if nums[fast] != val: nums[slow] = nums[fast] slow += 1 return slow leetcode 26.删除有序数组中的重复项 https://leetcode.cn/problems/remove-duplicates-from-sorted-array/ 代码 cpp: class Solution { public: int removeDuplicates(vector<int>& nums) { int fast; int slow = 0; for(fast = 1; fast < nums.size(); fast++){ if(nums[fast] != nums[slow]){ slow++; nums[slow] = nums[fast]; } } return slow + 1; } }; python: ...

2023年04月03日 · 2 分钟 · 596 字 · ZhaoYang

每日一题01

每日一题 二分查找 leetcode 704 二分查找 https://leetcode.cn/problems/binary-search/ 题解: CPP: class Solution { public: int search(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while(left < right){ int middle = left + right >> 1; if(nums[middle] >= target){ right = middle; }else{ left = middle + 1; } } if(nums[left] == target) return left; return -1; } }; class Solution { public: int search(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; while(left < right){ int middle = left + right + 1 >> 1; if(nums[middle] > target){ right = middle - 1; }else{ left = middle; } } if(nums[left] == target) return left; return -1; } }; python: class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0,len(nums) - 1 while left < right: middle = left + right + 1 >> 1 if nums[middle] > target: right = middle - 1 elif nums[middle] <= target: left = middle if nums[left] == target: return left return -1 class Solution: def search(self, nums: List[int], target: int) -> int: left, right = 0,len(nums) - 1 while left < right: middle = left + right >> 1 if nums[middle] >= target: right = middle elif nums[middle] < target: left = middle + 1 if nums[left] == target: return left return -1 leetcode 35.搜索插入位置 https://leetcode.cn/problems/search-insert-position/ ...

2023年04月02日 · 3 分钟 · 1028 字 · ZhaoYang