当前位置:网站首页>Li Kou today's question 926 Flip string to monotonic increment
Li Kou today's question 926 Flip string to monotonic increment
2022-06-12 17:05:00 【Struggling young man】
926. Flip the string to monotonic increment
Ideas :
Dynamic programming , State transition equation : When there is a 0 When , It can be done to 0 Conduct +1 Operate or make all of the preceding 1 become 0, State transition equation ret=min(ret + 1, num_1);
class Solution {
public int minFlipsMonoIncr(String s) {
char[] cs = s.toCharArray();
// First convert the string of words into an array ,ret Is used to record the minimum operand ,num_1 It's for recording 1 The number of
int n = cs.length, ret = 0 , num_1 = 0, num_0 = 0;
// for(int i = 0;i <n; i++){
// if(cs[i] == '0'){
// // encounter 0 There are two operations , One is to 0 Additive transformation 1, Operands +1. Or put this 0 Ahead 1 become 0, The operand depends on 1 The number of , That is to say num_1.
// ret = Math.min(ret+1,num_1);
// } else{
// // As long as you don't meet 0 That's it 1, therefore num_1 want ++ ,
// num_1++;
// }
// }
// The same way of thinking , This time it is traversal from back to front , encounter 1 There are two operations , One is to 1 become 0 Operands +1, The other is to put this 1 After that 0 All become one
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
边栏推荐
- Su directly switches to super administrator mode, so that many error reports can be avoided
- 每天5分钟玩转Kubernetes | 汇总
- Gerrit触发Jenkins SonarQube扫描
- The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
- R语言使用plot函数可视化数据散点图,使用font.axis参数指定坐标轴刻度标签的字体类型为斜体字体(italic)
- Contract awarding and AQS
- R语言计算data.table在一个分组变量的值固定的情况下另外一个分组变量下指定数值变量的均值
- (五)输出和输出
- The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti
- Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现
猜你喜欢

Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code

MySQL transaction introduction and transaction isolation level

Schrodinger's Japanese learning applet source code

Double write consistency problem

pytorch和torchvision官方文档使用方法

布局管理中的sizePolicy的策略问题

Extract the new Chinese cross modal benchmark zero from 5billion pictures and texts, and Qihoo 360's new pre training framework surpasses many SOTAS

邱盛昌:OPPO商业化数据体系建设实战

Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134) vulnerability recurrence

To understand Devops, you must read these ten books!
随机推荐
并发三色标记法
Preprocessing command section 3
Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)
key为断言的map是怎么玩的
大端模式和小端模式的区别
修改自己电脑连接的路由器的配置网址192.168.1.1
(七)循环语句for
Male god goddess voting source code v5.5.21 voting source code
Is the securities account opened by qiniu safe? Is it legal?
C # final review programming question (guessed by the teacher)
Swin transformer code explanation
redis. clients. jedis. exceptions. JedisConnectionException: Could not get a resource from the pool
邱盛昌:OPPO商业化数据体系建设实战
JS using RSA encryption and decryption
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
Loading shellcode in C and go languages
Some minor problems and solutions encountered when using ubantu
Pat class a 1139 first contact
Swin Transformer代码讲解
JVM memory model and local memory