70. 爬楼梯
int climbStairs(int n)
{
if (n <= 2) {
return n;
}
int ch2 = 1, ch1 = 2;
for (int i = 2; i < n; i++) {
int now = ch1 + ch2;
ch2 = ch1;
ch1 = now;
}
return ch1;
}
198. 打家劫舍
int rob(int* nums, int numsSize)
{
int p1 = 0, p2 =0;
for (int i = 0; i < numsSize; i++) {
int now = nums[i] + p2 > p1 ? nums[i] + p2 : p1;
p2 = p1;
p1 = now;
}
return p1;
}
213. 打家劫舍 II
int robCount(int* nums, int start, int end)
{
int p1 = 0, p2 = 0;
for (int i = start; i <= end; i++) {
int now = p2 + nums[i] > p1 ? p2 + nums[i] : p1;
p2 = p1;
p1 = now;
}
return p1;
}
int rob(int* nums, int numsSize)
{
if (numsSize == 1) {
return nums[0];
}
if (robCount(nums, 0 , numsSize - 2) > robCount(nums, 1, numsSize - 1)) {
return robCount(nums, 0 , numsSize - 2);
}
else {
return robCount(nums, 1, numsSize - 1);
}
}
64. 最小路径和
int minPathSum(int** grid, int gridSize, int* gridColSize)
{
int **dp = (int **)malloc(sizeof(int *) * gridSize);
for (int i = 0; i < gridSize; i++) {
dp[i] = (int *)malloc(sizeof(int) * (*gridColSize));
}
dp[0][0] = grid[0][0];
for (int i = 1; i < gridSize; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for (int j = 1; j < *gridColSize; j++) {
dp[0][j] = dp[0][j - 1] + grid[0][j];
}
for (int i = 1; i < gridSize; i++) {
for (int j = 1; j < *gridColSize; j++) {
if (dp[i][j - 1] <= dp[i - 1][j]) {
dp[i][j] = dp[i][j - 1] + grid[i][j];
} else {
dp[i][j] = dp[i - 1][j] + grid[i][j];
}
}
}
return dp[gridSize - 1][*gridColSize - 1];
}
62. 不同路径
int uniquePaths(int m, int n)
{
int** dp = (int**)malloc(sizeof(int*) * m);
for (int i = 0; i < m; i++) {
dp[i] = (int*)malloc(sizeof(int) * n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
}
else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
303. 区域和检索 - 数组不可变
typedef struct {
int a[10000];
} NumArray;
NumArray* numArrayCreate(int* nums, int numsSize) {
NumArray* a = (NumArray*)malloc(sizeof(NumArray));
if(numsSize == 0) {
return a;
}
a->a[0] = nums[0];
for(int i = 1; i < numsSize; i++) {
a->a[i] = nums[i] + a->a[i-1];
}
return a;
}
int numArraySumRange(NumArray* obj, int i, int j) {
if(i == 0) {
return obj->a[j];
}
return obj->a[j] - obj->a[i-1];
}
void numArrayFree(NumArray* obj) {
}
413. 等差数列划分
int numberOfArithmeticSlices(int* A, int ASize)
{
int* dp = (int*)calloc(ASize, sizeof(int));
int count = 0;
for (int i = 2; i < ASize; i++) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
dp[i] = dp[i - 1] + 1;
count += dp[i];
}
}
return count;
}
343. 整数拆分
int integerBreak(int n)
{
int* dp = (int*)malloc(sizeof(int) * (n + 1));
dp[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
int tmp = j * dp[i - j] > j * (i - j) ? j * dp[i - j] : j * (i - j);
dp[i] = dp[i] > tmp ? dp[i] : tmp;
}
}
return dp[n];
}
279. 完全平方数
int numSquares(int n)
{
int* dp = (int*)malloc(sizeof(int)*(n + 1));
dp[0] = 0;
dp[1] = 1;
for(int i = 2; i <= n; i++){
int min = INT_MAX;
int r = sqrt(i);
for(int j = 1; j <= r; j++){
min = min < 1 + dp[i - j * j] ? min : 1 + dp[i - j * j];
}
dp[i] = min;
}
return dp[n];
}
91. 解码方法
int numDecodings(char * s)
{
int scnt = strlen(s);
int* dp = (int*)malloc((scnt +1) *sizeof(int));
int cur, pre;
if(s[0] == '0') {
return 0;
}
if(scnt == 1) {
return 1;
}
dp[0] = 1, dp[1] = 1;
for(int i = 2; i<= scnt; i++) {
cur = (s[i-1] -'0');
pre = (s[i -2] -'0');
if((cur == 0) && ((pre >= 3) || (pre == 0))) {
return 0;
}
if(cur == 0) {
dp[i] = dp[i-2];
} else if(pre == 0) {
dp[i] = dp[i-1];
} else if((cur + pre * 10) <= 26) {
dp[i] = dp[i-2] + dp[i-1];
}
else {
dp[i] = dp[i-1];
}
}
return dp[scnt];
}
300. 最长上升子序列
int lengthOfLIS(int* nums, int numsSize)
{
if (numsSize < 1) {
return 0;
}
int* dp = (int*)malloc(sizeof(int) * numsSize);
for(int i = 0; i < numsSize; i++) {
dp[i] = 1;
}
int max = 1;
for (int i = 0; i < numsSize; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = dp[i] > (dp[j] + 1) ? dp[i] : (dp[j] + 1);
}
}
max = max > dp[i] ? max : dp[i];
}
return max;
}
646. 最长数对链
int cmp(const void* a, const void* b) {
int *aa = *(int**)a;
int *bb = *(int**)b;
if (aa[0] == bb[0]) {
return aa[1] - bb[1];
}
else {
return aa[0] - bb[0];
}
}
int findLongestChain(int** pairs, int pairsSize, int* pairsColSize){
int* dp = (int*)malloc(sizeof(int) * pairsSize);
int i, j;
int max = 0;
qsort(pairs, pairsSize, sizeof(pairs[0]), cmp);
for(i = 0; i < pairsSize; i++) {
dp[i] = 1;
}
for(i = 0; i < pairsSize; i++) {
for(j = 0; j < i; j++) {
if(pairs[i][0] > pairs[j][1]) {
dp[i] = dp[i] > (dp[j] + 1) ? dp[i] : (dp[j] + 1);
}
}
max = max > dp[i] ? max : dp[i];
}
return max;
}
376. 摆动序列
int wiggleMaxLength(int* nums, int numsSize)
{
if (numsSize < 1) {
return 0;
}
int up = 1, down = 1;
for (int i = 1; i < numsSize; i++) {
if (nums[i] > nums[i - 1]) {
up = down + 1;
}
else if (nums[i] < nums[i - 1]) {
down = up + 1;
}
}
return up > down ? up :down;
}
1143. 最长公共子序列
int longestCommonSubsequence(char * text1, char * text2)
{
int len1 = strlen(text1);
int len2 = strlen(text2);
int** dp = (int**)malloc(sizeof(int*) * (len1+1));
for (int i = 0; i < len1 + 1; i++) {
dp[i] = (int*)malloc(sizeof(int) * (len2 + 1));
}
for (int i = 0; i < len1 + 1; i++) {
dp[i][0] = 0;
}
for (int i = 0; i < len2 + 1; i++) {
dp[0][i] = 0;
}
for (int i = 1; i < len1 + 1; i++) {
for (int j = 1; j < len2 + 1; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else {
dp[i][j] = dp[i - 1][j] > dp[i][j - 1] ? dp[i-1][j] : dp[i][j-1];
}
}
}
return dp[len1][len2];
}
(背包)
309. 最佳买卖股票时机含冷冻期
#define MAX(x, y) (x) > (y) ? (x) : (y)
int maxProfit(int* prices, int pricesSize){
if(pricesSize <= 0 ){
return 0;
}
int temp_0 = 0 , temp_1 = INT_MIN , temp_2 = 0;
for(int i = 0 ; i < pricesSize; i++){
int tmp = temp_0;
temp_0 = MAX(temp_0, temp_1 + prices[i]);
temp_1 = MAX(temp_1, temp_2 - prices[i]);
temp_2 = tmp;
}
return temp_0;
}
714. 买卖股票的最佳时机含手续费
#define MAX(a, b) (a > b ? a : b)
int maxProfit(int* prices, int pricesSize, int fee)
{
if(pricesSize <= 1) {
return 0;
}
int dp0 = 0;
int dp1 = -prices[0] - fee;
for (int i = 0; i < pricesSize; i++) {
int temp = dp0;
dp0 = MAX(dp0, dp1 + prices[i]);
dp1 = MAX(dp1, temp - prices[i] - fee);
}
return dp0;
}
123. 买卖股票的最佳时机 III
#define MAX(a, b) (a > b ? a : b)
int maxProfit(int* prices, int pricesSize)
{
if(pricesSize <= 1) {
return 0;
}
int dp_i10 = 0, dp_i11 = -prices[0];
int dp_i20 = 0, dp_i21 = -prices[0];
for (int i = 1; i < pricesSize;i++) {
dp_i20 = MAX(dp_i20, dp_i21 + prices[i]);
dp_i21 = MAX(dp_i21, dp_i10 - prices[i]);
dp_i10 = MAX(dp_i10, dp_i11 + prices[i]);
dp_i11 = MAX(dp_i11, -prices[i]);
}
return dp_i20;
}
188. 买卖股票的最佳时机 IV
#define MAX(a, b) (a > b ? a : b)
int maxProfit2(int* prices, int pricesSize)
{
int dp0 = 0;
int dp1 = -prices[0];
for (int i = 1; i < pricesSize; i++) {
int temp = dp0;
dp0 = MAX(dp0, dp1 + prices[i]);
dp1 = MAX(dp1, temp - prices[i]);
}
return dp0;
}
int maxProfit(int k, int* prices, int pricesSize)
{
if (pricesSize <= 1) {
return 0;
}
if (k >= pricesSize / 2) {
return maxProfit2(prices, pricesSize);
}
int dp[pricesSize][k+1][2] ;
memset(dp, 0, sizeof(int) * pricesSize * (k + 1) * 2);
for (int i = 0; i < pricesSize; i++) {
for (int j = k; j >= 1; j--) {
if (i == 0) {
dp[i][j][0] = 0;
dp[i][j][1] = -prices[0];
continue;
}
dp[i][j][0] = MAX(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
dp[i][j][1] = MAX(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
}
return dp[pricesSize - 1][k][0];
}
583. 两个字符串的删除操作
int minDistance(char * word1, char * word2)
{
int len1 = strlen(word1);
int len2 = strlen(word2);
int** dp = (int**)malloc(sizeof(int*) * (len1 + 1));
for (int i = 0; i <= len1; i++) {
dp[i] = (int*)malloc(sizeof(int) * (len2 + 1));
}
for (int i = 0; i < len1 + 1; i++) {
dp[i][0] = 0;
}
for (int i = 0; i < len2 + 1; i++) {
dp[0][i] = 0;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i][j - 1] > dp[i - 1][j] ? dp[i][j - 1] : dp[i - 1][j];
}
}
}
return len1 + len2 - 2 * dp[len1][len2];
}
72. 编辑距离
#define Min(a, b) (a > b ? b : a)
int minDistance(char * word1, char * word2)
{
int len1 = strlen(word1);
int len2 = strlen(word2);
int** dp = (int**)malloc(sizeof(int*) * (len1 + 1));
for (int i = 0; i <= len1; i++) {
dp[i] = (int*)malloc(sizeof(int) * (len2 + 1));
}
dp[0][0] = 0;
for (int i = 1; i < len1 + 1; i++) {
dp[i][0] = i;
}
for (int i = 1; i < len2 + 1; i++) {
dp[0][i] = i;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Min(Min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]) + 1;
}
}
}
return dp[len1][len2];
}
650. 只有两个键的键盘
int minSteps(int n)
{
if (n == 1) {
return 0;
}
int* dp = (int*)calloc(n + 1, sizeof(int));
int h = (int)sqrt(n);
for (int i = 2; i <= n; i++) {
dp[i] = i;
for (int j = 2; j <= h; j++) {
if (i % j == 0) {
dp[i] = dp[j] + dp[i / j];
break;
}
}
}
return dp[n];
}