当前位置:网站首页>力扣今日题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
边栏推荐
- Add static route
- 890. 查找和替换模式 / 剑指 Offer II 080. 含有 k 个元素的组合
- Sudo of uabntu
- redis. clients. jedis. exceptions. JedisDataException: NOAUTH Authentication required
- Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
- Recommend AI intelligent drawing repair software
- 有趣的 LD_PRELOAD
- Where is it safer to open an account for thermal coal futures? How much is the thermal coal futures deposit?
- Iscc-2022 part WP
- ISCC-2022 部分wp
猜你喜欢

使用ubantu时,遇见的一些小毛病和解决方法

What's the matter with pbootcms' if judgment failure and direct display of labels?

2080虚拟机登录命令

2080 virtual machine login command

Double write consistency problem

性能优化之编译优化

Leetcode 2194. Excel 表中某个范围内的单元格(可以,已解决)

有趣的 LD_PRELOAD

Doctor application | National University of Singapore, Xinchao Wang, teacher recruitment, doctor / postdoctoral candidate in the direction of graph neural network

Idea how to set the guide package without * sign
随机推荐
并发三色标记法
使用ubantu时,遇见的一些小毛病和解决方法
Idea displays services on the console to uniformly manage all jetty services,
redis. clients. jedis. exceptions. JedisDataException: NOAUTH Authentication required
Token and idempotency
Why is your next computer a computer? Explore different remote operations
初识GO语言
Qcustomplot notes (I): qcustomplot adding data and curves
How to use the official documents of pytorch and torchvision
The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
Programmers broke the news: 3 job hopping in 4 years, and the salary has tripled! Netizen: the fist is hard
Su directly switches to super administrator mode, so that many error reports can be avoided
PAT甲级 1142 最大团
使用GCC的PGO(Profile-guided Optimization)优化整个系统
Play kubernetes every 5 minutes summary
软件工程 学生信息管理系统 结构化的需求分析
Qt开发高级进阶:初探qt + opengl
Leetcode 2194. Excel 表中某个范围内的单元格(可以,已解决)
程序员爆料:4年3次跳槽,薪资翻了3倍!网友:拳头硬了......
JS using RSA encryption and decryption