当前位置:网站首页>Leetcode298 weekly race record
Leetcode298 weekly race record
2022-06-27 05:17:00 【nth2000】
T3- Less than or equal to K The longest binary subsequence of


Ideas
The form of the optimal solution : Maximum length is m = ⌊ l o g 2 k ⌋ + 1 \lfloor log_2k\rfloor + 1 ⌊log2k⌋+1 Sequence + Put together the front All the leading 0. So find out as soon as possible from the back to the front m Bit less than or equal to k, In order to make the splicing leading 0 As much as possible .
If s Long for m The suffix of is less than or equal to k, Then the final answer is to use the suffix to splice all the preceding leading words 0; otherwise , Long for m The last digit of the suffix must be 1, So it is m-1 The suffix corresponds to a binary digit <k. It is the same as all the previous leads 0 Splicing
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;
}
};
边栏推荐
- 流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
- 重映像(STM32)
- 微服务系统设计——API 网关服务设计
- Golang Hello installation environment exception [resolved]
- Common programming abbreviations for orbit attitude
- [C language] keyword supplement
- 022 basics of C language: C memory management and C command line parameters
- 【B站UP DR_CAN学习笔记】Kalman滤波3
- AcWing 第 57 场周赛---BC题挺好
- 微服务系统设计——分布式事务服务设计
猜你喜欢

Microservice system design -- API gateway service design

Edge loads web pages in IE mode - edge sets ie compatibility

leetcode298周赛记录

双位置继电器RXMD2-1MRK001984 DC220V

微服务系统设计——API 网关服务设计

Terminal in pychar cannot enter the venv environment

stm32读取IO高低电平状态

微信小程序WebSocket使用案例

How pychart installs packages

RTP sending PS stream tool (open source)
随机推荐
018 basics of C language: C file reading and writing
010 C语言基础:C函数
AcWing 第 57 场周赛---BC题挺好
Avoid asteroids
Gao Xiang slam14 lecture - note 1
微服务系统设计——微服务调用设计
When STM32 turns off PWM output, it is a method to fix IO output at high or low level.
Asp.Net Core6 WebSocket 简单案例
Terminal in pychar cannot enter the venv environment
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
015 C语言基础:C结构体、共用体
DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
差点因为 JSON.stringify 丢了奖金...
three. JS first person camera gun following
Neo4j database export
016 C language foundation: C language enumeration type
006 C language foundation: C storage class
微服务系统设计——分布式锁服务设计
010 C language foundation: C function
清华大学开源软件镜像站网址