当前位置:网站首页>leetcode298周赛记录
leetcode298周赛记录
2022-06-27 05:13:00 【nth2000】
T3-小于等于 K 的最长二进制子序列


思路
最优解形式:最多长为m = ⌊ l o g 2 k ⌋ + 1 \lfloor log_2k\rfloor + 1 ⌊log2k⌋+1的序列 + 拼上前面所有的前导0。所以从后往前要尽早找出m位小于等于k,才能使拼接的前导0尽可能多。
如果s长为m的后缀小于等于k,则最终答案是用该后缀拼接上前面所有前导0;否则,长为m后缀最后一位一定是1,因此长为m-1后缀对应二进制数字<k.同样将其与前面所有前导0拼接
class Solution {
public:
int longestSubsequence(string s, int k) {
int count = 0;
int m = 32 - __builtin_clz(k);;
if(s.length() < m) return s.length();
for(int i = 0;i<s.length() - m;i++) if(s[i]== '0') count++;
long long temp = 0;
//for(int i = s.length() - 1;i>=s.length() - m;i--) temp = (long long)(s[i] - '0') << ((s.length() - i));
temp = stoi(s.substr(s.length() - m), nullptr, 2);
if(temp > (long long)k) return count + m - 1;
return count + m;
}
};
边栏推荐
- Py2neo basic syntax
- 笔记本电脑没有WiFi选项 解决办法
- Microservice system design -- microservice monitoring and system resource monitoring design
- Machunmei, the first edition of principles and applications of database... Compiled final review notes
- 013 C语言基础:C指针
- 007 basics of C language: C operator
- 双位置继电器XJLS-8G/220
- 牛客练习赛101-C 推理小丑---位运算+思维
- [622. design cycle queue]
- EasyExcel合并相同内容单元格及动态标题功能的实现
猜你喜欢

Zener diode zener diode sod123 package positive and negative distinction

【B站UP DR_CAN学习笔记】Kalman滤波2

Quick sort (non recursive) and merge sort

快速排序(非递归)和归并排序

Tsinghua University open source software mirror website

高翔slam14讲-笔记1

Penetration test - file upload / download / include

Pycharm 中 Terminal 无法进入 venv 环境的问题

Microservice system design - service fusing and degradation design

STM32 reads IO high and low level status
随机推荐
Mechanical transcoding journal [17] template, STL introduction
Redis高可用集群(哨兵、集群)
Machunmei, the first edition of principles and applications of database... Compiled final review notes
导航【机器学习】
第2章 关键技术介绍
流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
Gao Xiang slam14 lecture - note 1
Microservice system design -- microservice invocation design
019 C语言基础:C预处理
Quick sort (non recursive) and merge sort
什么是BFC?有什么用?
Reading graph augmentations to learn graph representations (lg2ar)
Golang Hello installation environment exception [resolved]
双位置继电器DLS-34A DC0.5A 220VDC
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
MySql最详细的下载教程
jq怎么获取倒数的元素
013 C语言基础:C指针
双位置继电器RXMVB2 R251 204 110DC
007 basics of C language: C operator