1. leetcode 509 斐波那契数
class Solution {
public :
int fib ( int n) {
if ( n <= 1 ) {
return n;
}
vector< int > dp ( n + 1 ) ;
dp[ 0 ] = 0 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++ ) {
dp[ i] = dp[ i - 1 ] + dp[ i - 2 ] ;
}
return dp[ n] ;
}
} ;
class Solution {
public :
int fib ( int n) {
if ( n < 2 ) {
return n;
}
int pre = 0 , cur = 1 ;
for ( int i = 2 ; i <= n; i++ ) {
int sum = pre + cur;
pre = cur;
cur = sum;
}
return cur;
}
} ;
2.leetcode 70 爬楼梯
class Solution {
public :
int climbStairs ( int n) {
if ( n <= 2 ) {
return n;
}
vector< int > dp ( n + 1 ) ;
dp[ 1 ] = 1 ;
dp[ 2 ] = 2 ;
for ( int i = 3 ; i <= n; i++ ) {
dp[ i] = dp[ i - 2 ] + dp[ i - 1 ] ;
}
return dp[ n] ;
}
} ;
class Solution {
public :
int climbStairs ( int n) {
if ( n <= 2 ) {
return n;
}
int pre = 1 , cur = 2 ;
for ( int i = 3 ; i <= n; i++ ) {
int sum = pre + cur;
pre = cur;
cur = sum;
}
return cur;
}
} ;
3.leetcode 746 使用最小花费爬楼梯
class Solution {
public :
int minCostClimbingStairs ( vector< int > & cost) {
int size = cost. size ( ) ;
vector< int > minCost ( size) ;
minCost[ 0 ] = 0 ;
minCost[ 1 ] = min ( cost[ 0 ] , cost[ 1 ] ) ;
for ( int i = 2 ; i < size; i++ ) {
minCost[ i] = min ( minCost[ i - 1 ] + cost[ i] , minCost[ i - 2 ] + cost[ i - 1 ] ) ;
}
return minCost[ size - 1 ] ;
}
} ;
class Solution {
public :
int minCostClimbingStairs ( vector< int > & cost) {
int minCost0 = 0 ;
int minCost1 = min ( cost[ 0 ] , cost[ 1 ] ) ;
int minCost;
for ( int i = 2 ; i < cost. size ( ) ; i++ ) {
minCost = min ( minCost1 + cost[ i] , minCost0 + cost[ i - 1 ] ) ;
minCost0 = minCost1;
minCost1 = minCost;
}
return minCost;
}
} ;
class Solution {
public :
int minCostClimbingStairs ( vector< int > & cost) {
vector< int > dp ( cost. size ( ) ) ;
dp[ 0 ] = cost[ 0 ] ;
dp[ 1 ] = cost[ 1 ] ;
for ( int i = 2 ; i < cost. size ( ) ; i++ ) {
dp[ i] = min ( dp[ i - 2 ] , dp[ i - 1 ] ) + cost[ i] ;
}
return min ( dp[ cost. size ( ) - 2 ] , dp[ cost. size ( ) - 1 ] ) ;
}
} ;
class Solution {
public :
int minCostClimbingStairs ( vector< int > & cost) {
for ( int i = 2 ; i < cost. size ( ) ; i++ ) {
cost[ i] = min ( cost[ i - 2 ] , cost[ i - 1 ] ) + cost[ i] ;
}
return min ( cost[ cost. size ( ) - 2 ] , cost[ cost. size ( ) - 1 ] ) ;
}
} ;
4.leetcode 62. 不同路径
class Solution {
public :
int uniquePaths ( int m, int n) {
vector< vector< int >> dp ( m, vector< int > ( n, 0 ) ) ;
for ( int i = 0 ; i < m; i++ ) {
dp[ i] [ 0 ] = 1 ;
}
for ( int j = 0 ; j < n; j++ ) {
dp[ 0 ] [ j] = 1 ;
}
for ( int i = 1 ; i < m; i++ ) {
for ( int j = 1 ; j < n; j++ ) {
dp[ i] [ j] = dp[ i - 1 ] [ j] + dp[ i] [ j - 1 ] ;
}
}
return dp[ m - 1 ] [ n - 1 ] ;
}
} ;
5. leetcode 63 不同路径 II
class Solution {
public :
int uniquePathsWithObstacles ( vector< vector< int >> & obstacleGrid) {
int m = obstacleGrid. size ( ) ;
int n = obstacleGrid[ 0 ] . size ( ) ;
vector< vector< int >> dp ( m, vector< int > ( n, 0 ) ) ;
for ( int i = 0 ; i < m && obstacleGrid[ i] [ 0 ] == 0 ; i++ ) {
dp[ i] [ 0 ] = 1 ;
}
for ( int j = 0 ; j < n && obstacleGrid[ 0 ] [ j] == 0 ; j++ ) {
dp[ 0 ] [ j] = 1 ;
}
for ( int i = 1 ; i < m; i++ ) {
for ( int j = 1 ; j < n; j++ ) {
if ( obstacleGrid[ i] [ j] == 1 ) {
continue ;
}
dp[ i] [ j] = dp[ i - 1 ] [ j] + dp[ i] [ j - 1 ] ;
}
}
return dp[ m - 1 ] [ n - 1 ] ;
}
} ;
6. leetcode 343 整数拆分
class Solution {
public :
int integerBreak ( int n) {
vector< int > dp ( n + 1 ) ;
dp[ 2 ] = 1 ;
for ( int i = 3 ; i <= n; i++ ) {
for ( int j = 1 ; j < i - 1 ; j++ ) {
dp[ i] = max ( dp[ i] , max ( ( i - j) * j, dp[ i - j] * j) ) ;
}
}
return dp[ n] ;
}
} ;
7. leetcode 96 不同的二叉搜索树
class Solution {
public :
int numTrees ( int n) {
vector< int > dp ( n + 1 ) ;
dp[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++ ) {
for ( int j = 1 ; j <= i; j++ ) {
dp[ i] += dp[ j - 1 ] * dp[ i - j] ;
}
}
return dp[ n] ;
}
} ;
# endif