/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classSolution { public: boolisValidBST(TreeNode* root) { if(root==NULL)returntrue; returnisv(r...
1/**2* Definition for binary tree3* struct TreeNode {4* int val;5* TreeNode *left;6* TreeNode *right;7* TreeNode(int x) : val(x), left(NULL), right(NULL) {}8* };9*/10classSolution {11public:12voidgetTree(vector<int> &result, TreeNode *root) {13if(!root)return;14getTr...
package leetcode // 解法一 栈 func longestValidParentheses(s string) int { stack, res := []int{}, 0 stack = append(stack, -1) for i := 0; i < len(s); i++ { if s[i] == '(' { stack = append(stack, i) } else { stack = stack[:len(stack)-1] if len(stack) ==...
0235-lowest-common-ancestor-of-a-binary-search-tree.scala 0242-valid-anagram.scala 0322-coin-change.scala 0347-top-k-frequent-elements.scala 0394-decode-string.scala 0435-non-overlapping-intervals.scala 0485-max-consecutive-ones.scala 0509-fibonacci-number.scala 0572-subtree-of-another-tree.scala ...
My Solutions to Leetcode problems. All solutions support C++ language, some support Java and Python. Multiple solutions will be given by most problems. Enjoy:) 我的Leetcode解答。所有的问题都支持C++语言,一部分问题支持Java语言。近乎所有问题都会提供多个算
LeetCode:Longest Valid Parentheses Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2....
LeetCode "Valid Perfect Square" Typical binary search.. but take care of data overflow if you are using C++ classSolution {public:boolisPerfectSquare(intnum) {if(num <0)returnfalse;if(num <2)returntrue;longlongi =1, j = num -1;while(i <=j)...
1964. 找出到每个位置为止最长的有效障碍赛跑路线 - 你打算构建一些障碍赛跑路线。给你一个 下标从 0 开始 的整数数组 obstacles ,数组长度为 n ,其中 obstacles[i] 表示第 i 个障碍的高度。 对于每个介于 0 和 n - 1 之间(包含 0 和 n - 1)的下标 i ,在满足下述条件
package leetcode func minAddToMakeValid(S string) int { if len(S) == 0 { return 0 } stack := make([]rune, 0) for _, v := range S { if v == '(' { stack = append(stack, v) } else if (v == ')') && len(stack) > 0 && stack[len(stack)-1] == '(' { sta...
LeetCode 98: Valid Binary Search Tree This answer is so awesome!! /*** Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/classSolution {...