摘要:
//深度优先搜索的方法在坏的情况下 时间复杂度会比较高,不过总的来说,如果只是找到一条可行的路径,一般也只会执行几次遍历。 如果题目是找最好情况,就需要动态规划保存多次重复执行的中间结果。 class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直 阅读全文
摘要:
动态转移方程 dp[pos] = min{dp[pos-k] +1} 当a[pos-k] >= k , k 是两次状态之间a的物理距离。 动态规划并不是这个例子的最好解法,时间复杂度 n^2, 空间复杂度有n, 在 n 比较大时,在有些平台并不能通过。 class Solution { public 阅读全文
摘要:
class Solution { public: int maxSubArray(vector<int>& nums) { int lastSum = nums[0]; //保存以上一个下标结尾的最大连续数组的和 int maxSum = nums[0]; // 保存最大的最大连续数组和 for(i 阅读全文
摘要:
对dp 变量需要执行初始化,否者LeetCode 会出现同样的用例,单独执行可以通过,提交代码执行不通过的情况。 下面是找最长回文串的动态规划代码。 class Solution { public: string longestPalindrome(string s) { int dp[1000][ 阅读全文
摘要:
#if 0 class Solution { //动态规划 public: int findLengthOfLCIS(vector<int>& nums) { vector<int> dp(nums.size()); int max = 0; for(int i = 0;i< nums.size() 阅读全文
摘要:
#include<iostream>#include<algorithm>#include<vector>using namespace std;class Solution {public: vector<string> permutation(string S) { sort(S.begin() 阅读全文
摘要:
#include<iostream>#include<vector>using namespace std; class Solution {public: void recursive(int n,int k,int value, int index,vector<int> &com_case, 阅读全文