14. 最长公共前缀
为保证权益,题目请参考 14. 最长公共前缀(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
int minCostClimbingStairs(vector<int> &cost)
{
if (cost.size() <= 2)
{
return 0;
}
int *dp = new int[cost.size()+1];
dp[0] = 0;
dp[1] = 0;
for (int i = 2; i <= cost.size(); ++i)
{
dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[cost.size()];
}
};
/*
Binayr search!!!
*/
bool binarySearch(vector<int> vec, int left, int right, int target)
{
if (left > right)
{
return false;
}
int middle = (left + right) / 2;
if (vec[middle] == target)
{
return true;
}
else
{
return binarySearch(vec, left, middle - 1, target) || binarySearch(vec, middle + 1, right, target);
}
}
int main()
{
vector<int> vec;
vec.push_back(4);
vec.push_back(2);
vec.push_back(3);
vec.push_back(1);
vec.push_back(5);
Solution so;
cout << so.lengthOfLIS(vec) << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59