摘要: /*** * 问题:分割等和子集 * 解决方法:动态规划、递归法: * 1、建立状态 * 2、状态转移方程 * 第一步、先求出和,判断其是否可以被2整除 * 第二步、for循环的确定dp * 递归法:时间会超时,函数的调度太大 * 动态规划:相当于背包的问题 * 在数组中找到一些数值去填充target * 从而可以确定状态boolean dp[i]表示target为i时用数组... 阅读全文
posted @ 2019-10-28 20:36 pycodego 阅读(339) 评论(0) 推荐(0) 编辑
摘要: #include <iostream> #include <vector> #include <algorithm> using namespace std; /*** * 分析:< 最优化问题 >此题用动态规划去写 * 1、第一步确定状态 >直接用它自己本身去写A[n][c] * 表示从第n层的第c个元素往下的最下的距离 * 2、第二步确定状态转移方程 * 状态转移方程可以分为三个部分 * 1、 阅读全文
posted @ 2019-10-27 23:23 pycodego 阅读(306) 评论(0) 推荐(0) 编辑
摘要: java:类似于c++的stl的unique的具体实现class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int i = 0; for (int j = 1; j < nums.length; j++) { if (nums[j] != nums[i]) { i++; 阅读全文
posted @ 2019-10-27 16:28 pycodego 阅读(254) 评论(0) 推荐(0) 编辑
摘要: java版:class Solution { private int low=0; private int high=0; public boolean isPerfectSquare(int num) { if(num==1)return true; low=0; high=num>>1; while(low<=high) { long mid = (low+high)>>1; long res 阅读全文
posted @ 2019-10-27 15:39 pycodego 阅读(485) 评论(0) 推荐(0) 编辑
摘要: c++版: class Solution { private: int low=-1; int high=-1; vector<int>src; public: vector<int> twoSum(vector<int>& numbers, int target) { low=0; high=numbers.size()-1; while(low<=high) { int res=numbers 阅读全文
posted @ 2019-10-27 14:50 pycodego 阅读(367) 评论(0) 推荐(0) 编辑
摘要: class Solution { public int mySqrt(int x) { // 注意:针对特殊测试用例,例如 2147395599 // 要把搜索的范围设置成长整型 // 为了照顾到 0 把左边界设置为 0 long left = 0; // # 为了照顾到 1 把右边界设置为 x // 2 + 1 long right = x / 2 + 1; //对于一个非负数n,它的平方根不会 阅读全文
posted @ 2019-10-27 12:47 pycodego 阅读(1526) 评论(0) 推荐(0) 编辑
摘要: 二分查找的例子:为下文做铺垫: c++: lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。 upper_bo 阅读全文
posted @ 2019-10-27 01:21 pycodego 阅读(176) 评论(0) 推荐(0) 编辑
摘要: class Solution { public TreeNode sortedArrayToBST(int[] nums) { return ToBST(nums,0,nums.length-1); } public static TreeNode ToBST(int nums[],int left,int right){ if(left>right)return null;/... 阅读全文
posted @ 2019-10-27 00:43 pycodego 阅读(179) 评论(0) 推荐(0) 编辑
摘要: //首先此题可以用动态规划去写 ----->逐个的去遍历每个元素作为根节点,左边的为左子树,右边的为右子树 ------特点是:左边的小于右边的,1-n正好是个有序的数组 //------->先确定状态 ----->以G(n)表示1--n总共的二叉搜索树的数量 ----->以F(i,n)表示以i的根的二叉搜索树的数量 //------>再确定状态转移方程 ------------>G(n)=∑... 阅读全文
posted @ 2019-10-26 15:29 pycodego 阅读(848) 评论(0) 推荐(0) 编辑
摘要: 暴力法:private int[][] data; public NumMatrix(int[][] matrix) { data = matrix; } public int sumRegion(int row1, int col1, int row2, int col2) { int sum = 0; for (int r = row1; r <= row2;... 阅读全文
posted @ 2019-10-26 14:17 pycodego 阅读(262) 评论(0) 推荐(0) 编辑