当前位置:网站首页>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;
}
};
边栏推荐
- Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
- Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
- ECMAScript6介绍及环境搭建
- Codeforces Round #648 (Div. 2) D. Solve The Maze
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- RT-Thread内核快速入门,内核实现与应用开发学习随笔记
- The location search property gets the login user name
- Huber Loss
- Illustrated network: what is gateway load balancing protocol GLBP?
- Codeworks round 639 (Div. 2) cute new problem solution
猜你喜欢
信息與熵,你想知道的都在這裏了
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
Count of C # LINQ source code analysis
3D reconstruction open source code summary [keep updated]
Halcon shape_ trans
容易混淆的基本概念 成员变量 局部变量 全局变量
Nodejs modularization
Rebuild my 3D world [open source] [serialization-1]
Confusing basic concepts member variables local variables global variables
优先级队列(堆)
随机推荐
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
12. Dynamic link library, DLL
How many checks does kubedm series-01-preflight have
Nodemon installation and use
kubeadm系列-02-kubelet的配置和启动
Halcon shape_ trans
scipy. misc. imread()
Halcon blob analysis (ball.hdev)
Nodejs modularization
使用arm Neon操作,提高内存拷贝速度
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Meta标签详解
MPSoC QSPI flash upgrade method
ROS learning 4 custom message
深入浅出PyTorch中的nn.CrossEntropyLoss
.NET服务治理之限流中间件-FireflySoft.RateLimit
Adaboost使用
[daiy4] copy of JZ35 complex linked list
混淆矩阵(Confusion Matrix)
My experience from technology to product manager