当前位置:网站首页>leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
2022-07-02 01:51:00 【重you小垃】



思路:贪心
具体细节:想找到小于k的最小子序列,因此思想是在前面补尽量多的0,贪心的思想,假设k有m位,取最后m位/m-1位,再加上前面所有的0,即为ans
class Solution {
public:
int longestSubsequence(string s, int k) {
int bits = 32 - __builtin_clz(k), n = s.size();
if (n < bits) return n;
int m = bits - 1;
string tmp = s.substr(n - bits);
if (stoi(tmp, nullptr, 2) <= k) m = bits;
return m + count_if(s.begin(), s.end() - m, [](char ch){
return ch == '0';});
}
};
代码难点:string按照二进制来转int :stoi(s, nullptr, 2)
边栏推荐
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- Five skills of adding audio codec to embedded system
- Memorabilia of domestic database in June 2022
- D discard the virtual recovery method
- Experimental reproduction of variable image compression with a scale hyperprior
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- Modeling essays series 124 a simple coding method
- 三分钟学会基础k线图知识
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- ES6 new method of string
猜你喜欢

人工智能在网络安全中的作用

How to use a product to promote "brand thrill"?

Should enterprises choose server free computing?

What are the skills of spot gold analysis?
![[rust web rokcet Series 1] Hello, world and get, post, put, delete](/img/d8/7dd5fe409d349a13128b6af554f952.jpg)
[rust web rokcet Series 1] Hello, world and get, post, put, delete

matlab 实现语音信号重采样和归一化,并播放比对效果

JMeter (II) - install the custom thread groups plug-in

Ks006 student achievement management system based on SSM

Raspberry pie 4B learning notes - IO communication (1-wire)

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
随机推荐
Based on configured schedule, the given trigger will never fire
Number of palindromes in C language (leetcode)
mysql列转行函数指的是什么
微信小程序中使用tabBar
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
2022 Q2 - 提升技能的技巧总结
如何远程、在线调试app?
Modeling essays series 124 a simple coding method
电子协会 C语言 1级 32、计算2的幂
1222. Password dropping (interval DP, bracket matching)
Based on configured schedule, the given trigger will never fire
Discussion on the idea of platform construction
Construction and maintenance of business websites [12]
What is AQS and its principle
牛客网——华为题库(51~60)
Exclusive delivery of secret script move disassembly (the first time)
正则表达式学习笔记
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
MPLS experiment operation
Matlab uses audioread and sound to read and play WAV files