当前位置:网站首页>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)
边栏推荐
- Five skills of adding audio codec to embedded system
- Architecture evolution from MVC to DDD
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- 迁移云计算工作负载的四个基本策略
- Construction and maintenance of business websites [13]
- Parted command
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- 如何远程、在线调试app?
猜你喜欢
Learn about servlets
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
matlab 使用 resample 完成重采样
This is the form of the K-line diagram (pithy formula)
Learn C language from scratch day 025 (maze)
What are the skills of spot gold analysis?
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
如何用一款产品推动「品牌的惊险一跃」?
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
How to debug apps remotely and online?
随机推荐
Exception handling of class C in yyds dry goods inventory
Regular expression learning notes
mysql列转行函数指的是什么
Number of palindromes in C language (leetcode)
Three core problems of concurrent programming
自动浏览拼多多商品
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
基于SSM实现微博系统
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
How to debug apps remotely and online?
Android: the kotlin language uses grendao3, a cross platform app development framework
ES6 new method of string
Matlab uses resample to complete resampling
Raspberry pie 4B learning notes - IO communication (1-wire)
跨域?同源?一次搞懂什么是跨域
游戏思考15:全区全服和分区分服的思考
Learning note 24 - multi sensor post fusion technology
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
2022 Q2 - 提升技能的技巧总结