当前位置:网站首页>力扣今日题926. 将字符串翻转到单调递增
力扣今日题926. 将字符串翻转到单调递增
2022-06-12 16:57:00 【抗争的小青年】
926. 将字符串翻转到单调递增
思路:
动态规划,状态转移方程: 当出现0的时候,可以对0进行+1操作或使得前面的所有1变成0, 状态转移方程 ret=min(ret + 1, num_1);
class Solution {
public int minFlipsMonoIncr(String s) {
char[] cs = s.toCharArray();
//先将字串串转成数组,ret是用来记录最小操作数的,num_1是用来记录1的个数的
int n = cs.length, ret = 0 , num_1 = 0, num_0 = 0;
// for(int i = 0;i <n; i++){
// if(cs[i] == '0'){
// //遇到0有两种操作,一个是将0加变成1,操作数+1。或者将这个0前面的1变成0,操作数取决于1的个数,也就是num_1.
// ret = Math.min(ret+1,num_1);
// } else{
// //只要没遇到0 那就是1,所以num_1要++ ,
// num_1++;
// }
// }
//思路一样,这次是从后往前遍历,遇到1有两种操作,一种是将1变成0操作数+1,另一种就是将这个1之后的0全部变成一
for(int i = n; i > 0 ; i--){
if(cs[i-1] == '1'){
ret = Math.min(ret + 1, num_0);
}else{
num_0++;
}
}
return ret;
}
}
2022/6/11
边栏推荐
- [Hunan University] information sharing of the first and second postgraduate entrance examinations
- 程序的动态加载和执行
- Pat class a 1142 largest regiment
- MySQL事务简介、事务隔离级别
- 初识GO语言
- Dynamic loading and execution of programs
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
- idea如何设置导包不带*号
- Picture online collection and delivery system source code
- Double write consistency problem
猜你喜欢

Picture online collection and delivery system source code

使用GCC的PGO(Profile-guided Optimization)优化整个系统

JVM内存模型与本地内存

男神女神投票源码 v5.5.21 投票源码

Gerrit触发Jenkins SonarQube扫描

redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required

博士申請 | 新加坡國立大學Xinchao Wang老師招收圖神經網絡方向博士/博後

Sudo of uabntu

How to use the official documents of pytorch and torchvision

Idea displays services on the console to uniformly manage all jetty services,
随机推荐
2080虚拟机登录命令
How to do a good job of testing in the company (do a good job of testing)
男神女神投票源码 v5.5.21 投票源码
890. find and replace mode / Sword finger offer II 080 Combination with k elements
邱盛昌:OPPO商业化数据体系建设实战
\Begin{algorithm} notes
Information outline recording tool: omnioutliner 5 Pro Chinese version
pytorch和torchvision官方文档使用方法
\Begin{algorithm} notes
selenium元素定位
Latex table online generation
Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS
Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
MySQL statement
JVM memory model and local memory
[raspberry pie]: (IV) camera advanced
Idea how to set the guide package without * sign
PAT甲级 1142 最大团
2080 virtual machine login command
(七)循环语句for