当前位置:网站首页>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)
边栏推荐
- Edge computing accelerates live video scenes: clearer, smoother, and more real-time
- 2022 Q2 - résumé des compétences pour améliorer les compétences
- Electronic Association C language level 1 33, odd even number judgment
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
- [Floyd] post disaster reconstruction
- Raspberry pie 4B learning notes - IO communication (1-wire)
- Based on configured schedule, the given trigger will never fire
- Feature extraction and detection 16 brisk feature detection and matching
- Based on configured schedule, the given trigger will never fire
- 电子协会 C语言 1级 32、计算2的幂
猜你喜欢

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

KS006基于SSM实现学生成绩管理系统

Redis有序集合如何使用

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

SQLite 3 of embedded database

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

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

Five skills of adding audio codec to embedded system

Four basic strategies for migrating cloud computing workloads

5g/4g pole gateway_ Smart pole gateway
随机推荐
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
[rust web rokcet Series 1] Hello, world and get, post, put, delete
The concept, function, characteristics, creation and deletion of MySQL constraints
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Construction and maintenance of business websites [12]
734. Energy stone (greed, backpack)
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Three core problems of concurrent programming
Using tabbar in wechat applet
matlab 实现语音信号重采样和归一化,并播放比对效果
How to debug apps remotely and online?
基于SSM实现微博系统
Ks006 student achievement management system based on SSM
遊戲思考15:全區全服和分區分服的思考
Five skills of adding audio codec to embedded system
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
Convolutional neural network (including code and corresponding diagram)