mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-13 01:11:29 +08:00
Update 动态规划设计:最长递增子序列.md
create problem-300 c++
This commit is contained in:
@ -215,4 +215,45 @@ public int lengthOfLIS(int[] nums) {
|
||||
<img src="../pictures/qrcode.jpg" width=200 >
|
||||
</p>
|
||||
|
||||
======其他语言代码======
|
||||
======其他语言代码======
|
||||
|
||||
[Kian](https://github.com/KianKw/) 提供 C++ 代码
|
||||
|
||||
```c++
|
||||
class Solution {
|
||||
public:
|
||||
int lengthOfLIS(vector<int>& nums) {
|
||||
/* len 为牌的数量 */
|
||||
int len = nums.size();
|
||||
vector<int> top(len, 0);
|
||||
/* 牌堆数初始化为0 */
|
||||
int piles = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
/* nums[i] 为要处理的扑克牌 */
|
||||
int poker = nums[i];
|
||||
|
||||
/***** 搜索左侧边界的二分查找 *****/
|
||||
int left = 0, right = piles;
|
||||
while (left < right) {
|
||||
int mid = left + (right - left) / 2;
|
||||
if (top[mid] > poker) {
|
||||
right = mid;
|
||||
} else if (top[mid] < poker) {
|
||||
left = mid + 1;
|
||||
} else if (top[mid] == poker) {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
/*********************************/
|
||||
|
||||
/* 没找到合适的牌堆,新建一堆 */
|
||||
if (left == piles)
|
||||
piles++;
|
||||
/* 把这张牌放到牌堆顶 */
|
||||
top[left] = poker;
|
||||
}
|
||||
/* 牌堆数就是 LIS 长度 */
|
||||
return piles;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
Reference in New Issue
Block a user