当前位置:网站首页>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)
边栏推荐
- 6-2 vulnerability exploitation - inevitable problems of FTP
- Learn C language from scratch day 025 (maze)
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
- 2022 Q2 - 提昇技能的技巧總結
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- 479. Additive binary tree (interval DP on the tree)
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- 并发编程的三大核心问题
- Memorabilia of domestic database in June 2022
- Is the knowledge of University useless and outdated?
猜你喜欢

MySQL如何解决delete大量数据后空间不释放的问题

Edge computing accelerates live video scenes: clearer, smoother, and more real-time

Learning note 3 -- Key Technologies of high-precision map (Part 1)

1222. Password dropping (interval DP, bracket matching)

开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?

The concept, function, characteristics, creation and deletion of MySQL constraints

【LeetCode 43】236. The nearest common ancestor of binary tree

Feature extraction and detection 16 brisk feature detection and matching

734. Energy stone (greed, backpack)

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
随机推荐
The concept, function, characteristics, creation and deletion of MySQL constraints
1218 square or round
Construction and maintenance of business websites [14]
I Brief introduction of radio energy transmission technology
三分钟学会基础k线图知识
Parted command
Matlab uses resample to complete resampling
电子协会 C语言 1级 32、计算2的幂
Electronic Society C language level 1 32, calculate the power of 2
uTools
What are the skills of spot gold analysis?
如何远程、在线调试app?
如何用一款产品推动「品牌的惊险一跃」?
MySQL如何解决delete大量数据后空间不释放的问题
Volume compression, decompression
Matlab uses audioread and sound to read and play WAV files
Modeling essays series 124 a simple coding method
What is AQS and its principle
KS006基于SSM实现学生成绩管理系统
2022 Q2 - résumé des compétences pour améliorer les compétences