摘要: //深度优先搜索的方法在坏的情况下 时间复杂度会比较高,不过总的来说,如果只是找到一条可行的路径,一般也只会执行几次遍历。 如果题目是找最好情况,就需要动态规划保存多次重复执行的中间结果。 class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直 阅读全文
posted @ 2022-09-06 19:28 danieldai 阅读(34) 评论(0) 推荐(0) 编辑
摘要: 动态转移方程 dp[pos] = min{dp[pos-k] +1} 当a[pos-k] >= k , k 是两次状态之间a的物理距离。 动态规划并不是这个例子的最好解法,时间复杂度 n^2, 空间复杂度有n, 在 n 比较大时,在有些平台并不能通过。 class Solution { public 阅读全文
posted @ 2022-09-06 19:15 danieldai 阅读(100) 评论(0) 推荐(0) 编辑
摘要: class Solution { public: int maxSubArray(vector<int>& nums) { int lastSum = nums[0]; //保存以上一个下标结尾的最大连续数组的和 int maxSum = nums[0]; // 保存最大的最大连续数组和 for(i 阅读全文
posted @ 2022-09-06 10:16 danieldai 阅读(12) 评论(0) 推荐(0) 编辑
摘要: 对dp 变量需要执行初始化,否者LeetCode 会出现同样的用例,单独执行可以通过,提交代码执行不通过的情况。 下面是找最长回文串的动态规划代码。 class Solution { public: string longestPalindrome(string s) { int dp[1000][ 阅读全文
posted @ 2022-09-05 16:49 danieldai 阅读(21) 评论(0) 推荐(0) 编辑
摘要: #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() 阅读全文
posted @ 2022-09-04 19:11 danieldai 阅读(22) 评论(0) 推荐(0) 编辑
摘要: #include<iostream>#include<algorithm>#include<vector>using namespace std;class Solution {public: vector<string> permutation(string S) { sort(S.begin() 阅读全文
posted @ 2022-09-04 01:15 danieldai 阅读(57) 评论(0) 推荐(0) 编辑
摘要: #include<iostream>#include<vector>using namespace std; class Solution {public: void recursive(int n,int k,int value, int index,vector<int> &com_case, 阅读全文
posted @ 2022-09-03 17:37 danieldai 阅读(19) 评论(0) 推荐(0) 编辑
摘要: strlen_test.cpp #include<iostream>#include<cstring>#include<time.h> using namespace std; /自定义strlen int my_strlen(char * str){ char* start = str; whil 阅读全文
posted @ 2022-06-16 02:02 danieldai 阅读(109) 评论(0) 推荐(0) 编辑
摘要: #include<iostream> using namespace std; template<typename T> struct comp_function{ bool operator()(const T &lhs,const T &rhs){ return lhs<rhs; }}; //比 阅读全文
posted @ 2022-06-15 00:21 danieldai 阅读(29) 评论(0) 推荐(0) 编辑
摘要: #include<iostream> using namespace std; //调整堆void adjust_heap(int *arr,int size,int pos){ int left = pos*2 +1; int right = pos*2 + 2; //左右两边先选取最小的,如果大 阅读全文
posted @ 2022-06-12 22:36 danieldai 阅读(21) 评论(0) 推荐(0) 编辑