当前位置:网站首页>leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
2022-07-02 01:54:00 【Heavy garbage】
Ideas : greedy
Specific details : Want to find less than k The youngest sequence of , Therefore, the thought is to supplement as much as possible in the front 0, Greedy thoughts , hypothesis k Yes m position , Take the last m position /m-1 position , Plus all the previous 0, That is to say 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';});
}
};
Code difficulties :string Turn according to binary int :stoi(s, nullptr, 2)
边栏推荐
- Laravel artisan 常用命令
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- Construction and maintenance of business websites [15]
- 【C#】使用正则校验内容
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- 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
- Game thinking 15: thinking about the whole region and sub region Services
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- There are spaces in the for loop variable in the shell -- IFS variable
- Memorabilia of domestic database in June 2022
猜你喜欢
MySQL约束与多表查询实例分析
Matlab uses resample to complete resampling
Memorabilia of domestic database in June 2022
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Redis环境搭建和使用的方法
ES6 new method of string
企业应该选择无服务器计算吗?
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
成功实现边缘编码需要了解的六大经验教训
Cross domain? Homology? Understand what is cross domain at once
随机推荐
Modeling essays series 124 a simple coding method
[C #] use regular verification content
uTools
Is the knowledge of University useless and outdated?
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
734. Energy stone (greed, backpack)
matlab 实现语音信号重采样和归一化,并播放比对效果
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
Android: the kotlin language uses grendao3, a cross platform app development framework
Introduction to ffmpeg Lib
Construction and maintenance of business websites [11]
Convolutional neural network (including code and corresponding diagram)
1069. Division of convex polygons (thinking, interval DP)
Using tabbar in wechat applet
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
【C#】使用正则校验内容
leetcode373. 查找和最小的 K 对数字(中等)
医药管理系统(大一下C语言课设)
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory