随笔分类 -  Leetcode

摘要:98. Validate Binary Search Tree class Solution { public: bool isValidBST(TreeNode* root) { return dfs(root,NULL,NULL); } // 此处要最大最小是因为BST需要比较的是全局的最大最小 阅读全文
posted @ 2021-03-06 18:11 张王李代茂 阅读(56) 评论(0) 推荐(0)
摘要:5. Longest Palindromic Substring: 最长回文子串 1) 中心扩散法: 分成奇偶由中间向两边拓展,取最长的 class Solution: def longestPalindrome(self, s: str) -> str: res = "" maxLen = 0 n 阅读全文
posted @ 2021-03-05 17:24 张王李代茂 阅读(94) 评论(0) 推荐(0)
摘要:反转链表 -迭代 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode tmp 阅读全文
posted @ 2021-03-04 06:50 张王李代茂 阅读(69) 评论(0) 推荐(0)
摘要:子集 class Solution { public: vector<vector<int>> res; vector<vector<int>> subsets(vector<int>& nums) { vector<int> sub; backtrack(nums,0,sub); return r 阅读全文
posted @ 2021-03-03 09:26 张王李代茂 阅读(124) 评论(0) 推荐(0)
摘要:99%的二分搜索问题都属于这3个模板中的1个。 时间复杂度:O(log n) 空间复杂度:O(1) 阅读全文
posted @ 2021-02-14 22:55 张王李代茂 阅读(51) 评论(0) 推荐(0)
摘要:class Solution { public: int findMin(vector<int>& nums) { int l = 0, r = nums.size()-1; int n = nums.size(); while(l<r){ int mid = (l+r)/2; int val = 阅读全文
posted @ 2021-02-11 03:44 张王李代茂 阅读(53) 评论(0) 推荐(0)
摘要:1 class Solution { 2 public: 3 int search(vector<int>& nums, int target) { 4 int l = 0, r = nums.size()-1; 5 while(l<=r){ 6 int mid = l + (r-l)/2; 7 i 阅读全文
posted @ 2021-02-10 23:02 张王李代茂 阅读(47) 评论(0) 推荐(0)