当前位置:网站首页>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;
}
};
边栏推荐
- DAST black box vulnerability scanner part 6: operation (final)
- Py2neo basic syntax
- Neo4j database export
- Tsinghua University open source software mirror website
- Vue学习笔记(五)Vue2页面跳转问题 | vue-router路由概念、分类与使用 | 编程式路由导航 | 路由组件的缓存 | 5种路由导航守卫 | 嵌套路由 | Vue2项目的打包与部署
- neo4j数据库导出
- 【FPGA】UART串口_V1.1
- Mechanical transcoding journal [17] template, STL introduction
- leetcode-20. Valid parentheses -js version
- 【622. 设计循环队列】
猜你喜欢

微服务系统设计——微服务调用设计

微服务系统设计——微服务监控与系统资源监控设计

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

How JQ gets the reciprocal elements

Microservice system design -- service registration, discovery and configuration design

golang hello 安装环境异常【已解决】

AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法

Microservice system design -- distributed transaction service design

躲避小行星游戏

STM32 reads IO high and low level status
随机推荐
Microservice system design -- distributed transaction service design
DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
Machunmei, the first edition of principles and applications of database... Compiled final review notes
021 basics of C language: recursion, variable parameters
021 C语言基础:递归,可变参数
STM32关闭PWM输出时,让IO输出固定高或低电平的方法。
Cognition - how to fill in 2022 college entrance examination volunteers
Some articles about component packaging and my experience
Discussion on streaming media protocol (MPEG2-TS, RTSP, RTP, RTCP, SDP, RTMP, HLS, HDS, HSS, mpeg-dash)
微服务系统设计——服务熔断和降级设计
STM32 MCU pin_ How to configure the pin of single chip microcomputer as pull-up input
jq怎么获取元素的id名
Microservice system design -- API gateway service design
pycharm 如何安装 package
ES6 0622 III
golang hello 安装环境异常【已解决】
stm32单片机引脚_如何将单片机的引脚配置为上拉输入
012 C语言基础:C数组
双位置继电器RXMVB2 R251 204 110DC
清华大学开源软件镜像站网址