当前位置:网站首页>2311. 小于等于 K 的最长二进制子序列
2311. 小于等于 K 的最长二进制子序列
2022-07-05 08:57:00 【紫菜(Nori)】
思路:
1.从右到左,寻找大于最大值的下标点
2.如果有剩余字符,只记录,为0的字符个数
class Solution {
public:
int longestSubsequence(string s, int k) {
int ans = 0;
// 记录当前总消息
long sum = 0;
// 倒序遍历元素,方便直接从右开始计算数字和
int len = s.size() - 1;
int i = len;
for(; i >= 0; i--){
// 当前位数的2的幂次
int bit = len - i;
if(s[i] == '0'){
// 如果当前的幂次所表示的数字,超过k,则可以直接返回,防止后面出现数字溢出问题
if(k < (sum + pow(2, bit))){
break;
}
// 否则继续
continue;
}
// 如果当前字符为1,则表示可以累加记数
// 当已经累加的数字 > k是跳出循环,当前的i保留在这个位置
if(k < (sum += pow(2, bit))){
break;
}
}
// 这个字符串表示的数字完全符合
if(i == -1){
return s.size();
}
// 从右到左,已经符合的长度,
ans = len - i;
// 由于表示的数字已经到达极限,这里只记录为零的字符即可
for(; i >= 0; i--){
if(s[i] == '1'){
continue;
}
ans++;
}
return ans;
}
};
边栏推荐
- Multiple linear regression (gradient descent method)
- location search 属性获取登录用户名
- 使用arm Neon操作,提高内存拷贝速度
- Halcon snap, get the area and position of coins
- 12. Dynamic link library, DLL
- 深度学习模型与湿实验的结合,有望用于代谢通量分析
- Infix expression evaluation
- Illustrated network: what is gateway load balancing protocol GLBP?
- [code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
- Adaboost使用
猜你喜欢
TF coordinate transformation of common components of ros-9 ROS
Redis实现高性能的全文搜索引擎---RediSearch
我从技术到产品经理的几点体会
编辑器-vi、vim的使用
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
Halcon snap, get the area and position of coins
Mathematical modeling: factor analysis
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Introduction Guide to stereo vision (1): coordinate system and camera parameters
随机推荐
Tips 1: Web video playback code
Digital analog 2: integer programming
C# LINQ源码分析之Count
OpenFeign
ABC#237 C
Pytorch entry record
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
优先级队列(堆)
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
Ros-11 common visualization tools
特征工程
kubeadm系列-01-preflight究竟有多少check
520 diamond Championship 7-4 7-7 solution
uni-app 实现全局变量
[daily training -- Tencent selected 50] 557 Reverse word III in string
Halcon snap, get the area and position of coins
Array,Date,String 对象方法
asp.net(c#)的货币格式化
Meta tag details
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)