当前位置:网站首页>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;
}
};边栏推荐
- Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
- nodejs_ 01_ fs. readFile
- Nodemon installation and use
- Meta tag details
- Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
- Kubedm series-00-overview
- 浅谈Label Smoothing技术
- Programming implementation of ROS learning 5-client node
- 牛顿迭代法(解非线性方程)
- kubeadm系列-01-preflight究竟有多少check
猜你喜欢

Halcon shape_ trans
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

Halcon snap, get the area and position of coins

Attention is all you need

TF coordinate transformation of common components of ros-9 ROS
![Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]](/img/7d/e7370e757c18b3dbb47e633703c346.jpg)
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]

Ros-10 roslaunch summary

Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)

Use and programming method of ros-8 parameters

Confusion matrix
随机推荐
Halcon Chinese character recognition
阿里云发送短信验证码
Kubedm series-00-overview
Halcon shape_ trans
520 diamond Championship 7-4 7-7 solution
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
OpenFeign
notepad++
Huber Loss
Mengxin summary of LIS (longest ascending subsequence) topics
Mengxin summary of LCs (longest identical subsequence) topics
Editor use of VI and VIM
ORACLE进阶(三)数据字典详解
My experience from technology to product manager
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
np.allclose
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Programming implementation of ROS learning 2 publisher node
uni-app 实现全局变量
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis