当前位置:网站首页>力扣今日题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
边栏推荐
- \begin{algorithm} 笔记
- Différence entre le mode grand et le mode petit
- Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
- C # final review programming question (guessed by the teacher)
- \Begin{algorithm} notes
- pytorch和torchvision官方文档使用方法
- Swin Transformer代码讲解
- JVM memory model and local memory
- Three paradigms of database
- 大端模式和小端模式的区别
猜你喜欢

Advanced Qt development: a preliminary study QT + OpenGL

Google browser debugging skills

pytorch和torchvision官方文档使用方法

MySQL事务简介、事务隔离级别

D. master router setting and 401 networking

Double write consistency problem

Modify the configuration of the router connected to your computer. The website is 192.168.1.1

Programmers broke the news: 3 job hopping in 4 years, and the salary has tripled! Netizen: the fist is hard

性能优化之编译优化

如何查看、修改和删除SSH
随机推荐
The C programming language (version 2) notes / 8 UNIX system interface / 8.7 instance (storage allocator)
Swin Transformer代码讲解
Token and idempotency
SwinTransformer网络架构
890. find and replace mode / Sword finger offer II 080 Combination with k elements
Canvas image processing (Part 1)
Qcustomplot notes (I): qcustomplot adding data and curves
What is compound interest financial product?
Gerrit触发Jenkins SonarQube扫描
su直接切换到超级管理员模式,这样很多报错都可以避免了
MySQL statement
Doctor application | National University of Singapore, Xinchao Wang, teacher recruitment, doctor / postdoctoral candidate in the direction of graph neural network
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
\begin{algorithm} 笔记
Idea how to set the guide package without * sign
Double write consistency problem
Swin transformer code explanation
The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
How to play the map with key as assertion
Uniapp壁纸小程序源码/双端微信抖音小程序源码