当前位置:网站首页>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;
}
};
边栏推荐
- 编辑器-vi、vim的使用
- Array, date, string object method
- [daiy4] jz32 print binary tree from top to bottom
- Halcon wood texture recognition
- 3D reconstruction open source code summary [keep updated]
- It cold knowledge (updating ing~)
- nodejs_ fs. writeFile
- Meta标签详解
- Rebuild my 3D world [open source] [serialization-1]
- Codeforces Round #648 (Div. 2) E.Maximum Subsequence Value
猜你喜欢
Beautiful soup parsing and extracting data
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
Redis implements a high-performance full-text search engine -- redisearch
Attention is all you need
Codeworks round 639 (Div. 2) cute new problem solution
RT-Thread内核快速入门,内核实现与应用开发学习随笔记
信息與熵,你想知道的都在這裏了
Halcon color recognition_ fuses. hdev:classify fuses by color
C [essential skills] use of configurationmanager class (use of file app.config)
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
随机推荐
ECMAScript6介绍及环境搭建
某公司文件服务器迁移方案
golang 基础 ——map、数组、切片 存放不同类型的数据
[beauty of algebra] solution method of linear equations ax=0
. Net service governance flow limiting middleware -fireflysoft RateLimit
Summary of "reversal" problem in challenge Programming Competition
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Pearson correlation coefficient
np.allclose
Count of C # LINQ source code analysis
阿里云发送短信验证码
location search 属性获取登录用户名
File server migration scheme of a company
容易混淆的基本概念 成员变量 局部变量 全局变量
Ros-11 common visualization tools
Nodemon installation and use
asp. Net (c)