当前位置:网站首页>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;
}
};边栏推荐
- Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
- [牛客网刷题 Day4] JZ32 从上往下打印二叉树
- Codeworks round 639 (Div. 2) cute new problem solution
- My university
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- Multiple linear regression (gradient descent method)
- 2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
- Codeforces Round #648 (Div. 2) D. Solve The Maze
- Programming implementation of ROS learning 2 publisher node
- .NET服务治理之限流中间件-FireflySoft.RateLimit
猜你喜欢

我从技术到产品经理的几点体会

Confusing basic concepts member variables local variables global variables

Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)

资源变现小程序添加折扣充值和折扣影票插件

Yolov4 target detection backbone

Programming implementation of subscriber node of ROS learning 3 subscriber

Typescript hands-on tutorial, easy to understand

微信H5公众号获取openid爬坑记
![[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization

Digital analog 1: linear programming
随机推荐
资源变现小程序添加折扣充值和折扣影票插件
Business modeling of software model | overview
asp.net(c#)的货币格式化
3D reconstruction open source code summary [keep updated]
My experience from technology to product manager
Count of C # LINQ source code analysis
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
Golang foundation -- map, array and slice store different types of data
图解网络:什么是网关负载均衡协议GLBP?
[牛客网刷题 Day4] JZ32 从上往下打印二叉树
Oracle advanced (III) detailed explanation of data dictionary
2011-11-21 training record personal training (III)
Jenkins Pipeline 方法(函数)定义及调用
Halcon color recognition_ fuses. hdev:classify fuses by color
MPSoC QSPI Flash 升级办法
Basic number theory - factors
Programming implementation of ROS learning 6 -service node
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
Program error record 1:valueerror: invalid literal for int() with base 10: '2.3‘
js异步错误处理