当前位置:网站首页>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;
}
};边栏推荐
- asp.net(c#)的货币格式化
- Meta标签详解
- 迁移学习和域自适应
- ROS learning 1- create workspaces and function packs
- Programming implementation of ROS learning 2 publisher node
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- MPSoC QSPI Flash 升级办法
- 多元线性回归(梯度下降法)
- Halcon affine transformations to regions
- Huber Loss
猜你喜欢

Redis实现高性能的全文搜索引擎---RediSearch

Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method

Huber Loss

Generate confrontation network

信息与熵,你想知道的都在这里了

Beautiful soup parsing and extracting data

Huber Loss

生成对抗网络

Halcon blob analysis (ball.hdev)
![Rebuild my 3D world [open source] [serialization-2]](/img/e6/aad5f432aca619b992753187729dcf.jpg)
Rebuild my 3D world [open source] [serialization-2]
随机推荐
C#图像差异对比:图像相减(指针法、高速)
2311. 小于等于 K 的最长二进制子序列
12、动态链接库,dll
Ros-10 roslaunch summary
uni-app 实现全局变量
It cold knowledge (updating ing~)
Halcon wood texture recognition
. Net service governance flow limiting middleware -fireflysoft RateLimit
浅谈Label Smoothing技术
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
Beautiful soup parsing and extracting data
Programming implementation of subscriber node of ROS learning 3 subscriber
Huber Loss
L'information et l'entropie, tout ce que vous voulez savoir est ici.
nodejs_ 01_ fs. readFile
Mengxin summary of LCs (longest identical subsequence) topics
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Halcon color recognition_ fuses. hdev:classify fuses by color
Meta标签详解
File server migration scheme of a company