当前位置:网站首页>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;
}
};边栏推荐
- Confusion matrix
- Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
- Programming implementation of ROS learning 2 publisher node
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- 编辑器-vi、vim的使用
- 深入浅出PyTorch中的nn.CrossEntropyLoss
- Programming implementation of ROS learning 5-client node
- Nodejs modularization
- 使用arm Neon操作,提高内存拷贝速度
- File server migration scheme of a company
猜你喜欢
![Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]](/img/ed/0483c529db2af5b16b18e43713d1d8.jpg)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]

Halcon: check of blob analysis_ Blister capsule detection

Solutions of ordinary differential equations (2) examples

2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition

Programming implementation of ROS learning 6 -service node

Halcon snap, get the area and position of coins
![一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
![Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]](/img/d8/39020b1ce174299f60b6f278ae0b91.jpg)
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]

什么是防火墙?防火墙基础知识讲解

Nodemon installation and use
随机推荐
Pearson correlation coefficient
TF coordinate transformation of common components of ros-9 ROS
AdaBoost use
什么是防火墙?防火墙基础知识讲解
深入浅出PyTorch中的nn.CrossEntropyLoss
Codeworks round 638 (Div. 2) cute new problem solution
scipy.misc.imread()
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Golang foundation - the time data inserted by golang into MySQL is inconsistent with the local time
微信H5公众号获取openid爬坑记
My experience from technology to product manager
Count of C # LINQ source code analysis
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Use arm neon operation to improve memory copy speed
Solutions of ordinary differential equations (2) examples
Beautiful soup parsing and extracting data
Attention is all you need
[beauty of algebra] solution method of linear equations ax=0
Wechat H5 official account to get openid climbing account
fs. Path module