当前位置:网站首页>力扣今日题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} notes
- \begin{algorithm} 笔记
- [Hunan University] information sharing of the first and second postgraduate entrance examinations
- Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
- Preprocessing command section 3
- JS using RSA encryption and decryption
- Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
- (五)输出和输出
- Concurrent trichromatic marking
- Difference between big end mode and small end mode
猜你喜欢

图片在线收款发货系统源码

canvas 高级功能(下)

RMI, JNDI, LDAP introduction +log4j vulnerability analysis

薛定谔的日语学习小程序源码

JVM内存模型与本地内存

Swin Transformer代码讲解

Information outline recording tool: omnioutliner 5 Pro Chinese version

如何查看、修改和删除SSH

快速入门scrapy爬虫框架

Modify the configuration of the router connected to your computer. The website is 192.168.1.1
随机推荐
邱盛昌:OPPO商业化数据体系建设实战
[adult Liu Er - pytorch deep learning practice] notes with learning (I)
[research] reading English papers -- the welfare of researchers in English poor
JVM memory model and local memory
Canvas advanced functions (Part 2)
性能优化之编译优化
Leetcode 2190. 数组中紧跟 key 之后出现最频繁的数字(可以,一次过)
QCustomplot笔记(一)之QCustomplot添加数据以及曲线
博士申請 | 新加坡國立大學Xinchao Wang老師招收圖神經網絡方向博士/博後
The C programming language (version 2) notes / 8 UNIX system interface / 8.4 random access (lseek)
canvas 高级功能(下)
从50亿图文中提取中文跨模态新基准Zero,奇虎360全新预训练框架超越多项SOTA
有趣的 LD_PRELOAD
大端模式和小端模式的區別
Qcustomplot notes (I): qcustomplot adding data and curves
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
Recommend 6 open source projects of yyds
Double write consistency problem
Three paradigms of database
Idea how to set the guide package without * sign