怎样在LeetCode 410题中找到最优分割点? 这道题看着好像没什么思路,但其实可以利用二分法来做,二分法中的mid就是最终要返回的值,也就代表着子数组的和最小的值 我们首先还是设置左右区间,左区间L=0,右区间是数组所有元素的和再加1,因为二分法的区间一般是左闭右开 然后就是将数组进行打包,从第一个...
我的思路:bitset的使用,http://blog.csdn.net/qll125596718/article/details/6901935 我的代码: View Code solution解法: View Code 386. Lexicographical Numbers 题意:按字典序输出1~n 我的思路:找规律 我的代码: View Code solution解法:好一些 View Code 417. Pacific Atlantic Water Flow 题意:每个位置能往...
public class Solution { public String slidingWindow(String s, String t) { // 起始的时候,都位于 0,同方向移动 int left = 0; int right = 0; while (right < sLen) { if ( 在右移的过程中检测是否满足条件 ) { // 对状态做修改,好让程序在后面检测到满足条件 } // 右边界右移 1 格 right...
public class Solution { public String minWindow(String s, String t) { // 起始的时候,都位于 0,同方向移动 int left = 0; int right = 0; while (right < sLen) { if ( 在右移的过程中检测是否满足条件 ) { // 对状态做修改,好让程序在后面检测到满足条件 } // 右边界右移 1 格 right++...
classSolution{publicintmaxDepth(String s){Stack<Character>stack=newStack<>();int result=0;for(int i=0;i<s.length();i++){if(s.charAt(i)=='('){stack.push('(');}elseif(s.charAt(i)==')'){result=Math.max(result,stack.size());stack.pop();}}returnresult;}} ...
publicclassSolution{publicStringminWindow(Strings,Stringt) {// 起始的时候,都位于 0,同方向移动intleft=0;intright=0;while(right<sLen) {if(在右移的过程中检测是否满足条件) {// 对状态做修改,好让程序在后面检测到满足条件}// 右边界右移 1 格right++;while(满足条件) {// 走到这里是满足条件的,...
1classSolution {2public:3intfindPeakElement(vector<int>&nums) {4constintn =nums.size();5intleft =0, right = n-1;6while(left <right) {7intmid = (left + right) /2;8if(nums[mid] < nums[mid+1]) {9left = mid +1;10}else{11right =mid;12}13}14returnleft;15}16}; ...
class Solution { public int climbStairs(int n) { int[] memo = new int[...
class Solution: vector<int> postorderTraversal(TreeNode *root){ vector<int> res; if(root==nullptr) { return res; } stack<TreeNode*> sta; // 保存前一个访问的节点,用于确定当前节点的右子树是否访问完毕 TreeNode * prev; // 根节点或栈不为空 while(root || !sta.empty()) { while(root)...
class Solution { public: int trap(vector<int>& height) { if(height.empty()) return 0; height.push_back(0); int res =0; stack<int> st;//decreasing stack for(int i = 0; i<height.size();++i){ while(!st.empty() && height[i] > height[st.top()]){ ...