当前位置:网站首页>2311. Longest binary subsequence less than or equal to K
2311. Longest binary subsequence less than or equal to K
2022-07-05 09:06:00 【Laver (nori)】
Ideas :
1. From right to left , Look for lower punctuation greater than the maximum
2. If there are remaining characters , Record only , by 0 Characters of
class Solution {
public:
int longestSubsequence(string s, int k) {
int ans = 0;
// Record the current total message
long sum = 0;
// Traverse the elements in reverse order , It is convenient to calculate numbers and... Directly from the right
int len = s.size() - 1;
int i = len;
for(; i >= 0; i--){
// Current digit 2 The power of
int bit = len - i;
if(s[i] == '0'){
// If the number represented by the current power , exceed k, You can go back to , Prevent the following number overflow problem
if(k < (sum + pow(2, bit))){
break;
}
// Otherwise continue
continue;
}
// If the current character is 1, Then it means that the number can be accumulated
// When the accumulated number > k It's about jumping out of the loop , Current i Keep in this position
if(k < (sum += pow(2, bit))){
break;
}
}
// The number represented by this string completely conforms to
if(i == -1){
return s.size();
}
// From right to left , Length already met ,
ans = len - i;
// Because the number represented has reached the limit , Only zero characters are recorded here
for(; i >= 0; i--){
if(s[i] == '1'){
continue;
}
ans++;
}
return ans;
}
};边栏推荐
- 3D reconstruction open source code summary [keep updated]
- Characteristic Engineering
- scipy. misc. imread()
- Return of missing persons
- 嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
- Introduction Guide to stereo vision (7): stereo matching
- 什么是防火墙?防火墙基础知识讲解
- 皮尔森相关系数
- Blogger article navigation (classified, real-time update, permanent top)
- Applet (subcontracting)
猜你喜欢

Redis实现高性能的全文搜索引擎---RediSearch
![[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization

生成对抗网络

Programming implementation of subscriber node of ROS learning 3 subscriber

混淆矩阵(Confusion Matrix)
![一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

RT-Thread内核快速入门,内核实现与应用开发学习随笔记

信息与熵,你想知道的都在这里了
![[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation](/img/d8/7291a5b14160600ba73810e6dd1eb5.jpg)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
随机推荐
Halcon wood texture recognition
JS asynchronous error handling
C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
OpenFeign
信息与熵,你想知道的都在这里了
np.allclose
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Use and programming method of ros-8 parameters
Adaboost使用
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
Characteristic Engineering
Summary of "reversal" problem in challenge Programming Competition
ORACLE进阶(三)数据字典详解
Multiple linear regression (gradient descent method)
皮尔森相关系数
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
asp.net(c#)的货币格式化
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
Meta tag details