未验证 提交 84569b8c 编写于 作者: K Kian Kwok 提交者: GitHub

Update 动态规划设计:最长递增子序列.md

create problem-300 c++
上级 8b8f4135
......@@ -215,4 +215,45 @@ public int lengthOfLIS(int[] nums) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
[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;
}
};
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册