当前位置:网站首页>Leetcode 926. 将字符串翻转到单调递增 [前缀和]
Leetcode 926. 将字符串翻转到单调递增 [前缀和]
2022-06-13 02:17:00 【JackDawn!?】
题目

思路
我们要求解的字符串具有的性质是,存在一个位于s[i],s[i+1]之前的位置,这个位置左边的字符全为0,右边的字符全是1 。我们可以通过翻转某些值构造出一个满足上述性质的字符串,所以我们要找的就是这样一个位置,使得它左边1的数目加上右边0的数目最小。
代码
class Solution {
private:
int* pre;
public:
int minFlipsMonoIncr(string s) {
pre = new int[s.size()+1];
pre[0] = 0;
for(int i = 0; i < s.size(); ++i) {
pre[i+1] = pre[i]+s[i]-'0';
}
int _min = INT_MAX;
for(int i = -1; i < (int)s.size(); ++i) {
// 从-1开始是因为这个位置可能位于所有字符之前
// 由于s.size()返回的是无符号数,所以要强制类型转换
int t = s.size()-(i+1);
t -= (pre[s.size()]-pre[i+1]);
_min = min(_min, pre[i+1]+t);
/* printf("%d %d\n", pre[i+1], t); */
}
return _min;
}
};
class Solution {
// 刚发现我sb了,下面这样都不用计算前缀和,思维定式可太可怕了
public:
int minFlipsMonoIncr(string s) {
int lo = 0, rz = 0;
for(int i = 0; i < s.size(); ++i) {
if(s[i]=='0') ++rz;
}
int _min = lo+rz;
for(int i = 0; i < s.size(); ++i) {
if(s[i]=='1') {
lo += 1;
} else {
rz -= 1;
}
_min = min(lo+rz, _min);
}
return _min;
}
};
运行结果

边栏推荐
- Chapter7-13_ Dialogue State Tracking (as Question Answering)
- Classification and summary of system registers in aarch64 architecture of armv8/arnv9
- Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
- Huawei equipment is configured with dual reflectors to optimize the backbone layer of the virtual private network
- LabVIEW large project development tools to improve quality
- synchronized下的 i+=2 和 i++ i++执行结果居然不一样
- Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
- About the fact that I gave up the course of "Guyue private room course ROS manipulator development from introduction to actual combat" halfway
- 记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
- Bai ruikai Electronic sprint Scientific Innovation Board: proposed to raise 360 million Funds, Mr. And Mrs. Wang binhua as the main Shareholder
猜你喜欢

(novice to) detailed tutorial on machine / in-depth learning with colab from scratch
![[learning notes] xr872 audio driver framework analysis](/img/1a/008a89f835dc1b350a1f1ff27bee00.jpg)
[learning notes] xr872 audio driver framework analysis

Huawei equipment configures private IP routing FRR

Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders

Bai ruikai Electronic sprint Scientific Innovation Board: proposed to raise 360 million Funds, Mr. And Mrs. Wang binhua as the main Shareholder

ROS learning-7 error in custom message or service reference header file

Cumulative tax law: calculate how much tax you have paid in a year

华为设备配置虚拟专用网FRR

Mac使用Docker安装Oracle

C language conditional compilation routine
随机推荐
Jump model between mirrors
【Unity】打包WebGL項目遇到的問題及解决記錄
SWD debugging mode of stm32
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
[programming idea] communication interface of data transmission and decoupling design of communication protocol
Paper reading - jukebox: a generic model for music
Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
STM32 timer interrupt learning notes
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]
Basic exercises of test questions letter graphics ※
Huawei equipment is configured with CE dual attribution
Introduction to easydl object detection port
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
C language volatile learning
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
Ctrip reshapes new Ctrip
js获取元素
Deep learning the principle of armv8/armv9 cache