LeetCode:926. 将字符串翻转到单调递增

暴力法超时:思想:动态规划
public int minFlipsMonoIncrb(String S) {
int result = S.length();
for (int i = 0; i < S.length(); i++) {
char[] str1 = S.substring(0, i).toCharArray();
char[] str2 = S.substring(i + 1, S.length()).toCharArray();
int zero = 0;
int one = 0;
for (int j = 0; j < str1.length; j++) {
if (str1[j] == '1')
zero++;
}
for (int j = 0; j < str2.length; j++) {
if (str2[j] == '0')
one++;
}
int re = zero + one;
if (re < result)
result = re;
}
return result;
}
优化后:
public int minFlipsMonoIncr(String S) {
int result = S.length();
int zero = 0;
int leftOne = 0;
int rightZero = 0;
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == '0')
zero++;
}
rightZero = zero;
for (int i = -1; i < S.length(); i++) {
if (i == -1) {
} else {
if (S.charAt(i) == '0') {
rightZero--;
}
if (S.charAt(i) == '1') {
leftOne++;
}
}
int re = leftOne + rightZero;
if (re < result)
result = re;
}
return result;
}
LeetCode:926. 将字符串翻转到单调递增的更多相关文章
- [Swift]LeetCode926. 将字符串翻转到单调递增 | Flip String to Monotone Increasing
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...
- [LeetCode] Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [LeetCode] 738. Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- 【LeetCode】738. 单调递增的数字
738. 单调递增的数字 知识点:字符串:贪心 题目描述 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x ...
- Java实现 LeetCode 738 单调递增的数字(暴力)
738. 单调递增的数字 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= ...
- 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹
一, 最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...
- 【LCS,LIS】最长公共子序列、单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
- nyoj 17 单调递增最长子序列
单调递增最长子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4 输入 ...
随机推荐
- 更新portage之后 安装 certbot
运行的时候一直报如下的错误: sudo certbot 错误结果: Traceback (most recent call last): File "/usr/lib/python-exec ...
- 【OS_Linux】Linux系统中目录及文件管理
1.Linux系统中目录的树状结构 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录, ...
- python爬虫基础02-urllib库
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- Day05基本运算符,if判断和while循环
day05 1.常量 变量名全大写 2.基本运算符 ①算术运算 10/3除法 10//3取整 10*3乘法 10**3幂 ②赋值运算 增量赋值 age += 1#age = age + 1 age * ...
- 生物信息学练习2- Biom-format
The Biological Observation Matrix (BIOM) format http://biom-format.org/ biom-format有两种方式安装: 1. pytho ...
- Android开发——Android的消息机制详解
)子线程默认是没有Looper的,Handler创建前,必须手动创建,否则会报错.通过Looper.prepare()即可为当前线程创建一个Looper,并通过Looper.loop()来开启消息循环 ...
- 【js】--常用DOM库工具
/* 2014年3月16号 常用DOM工具库*/var DOM={}; DOM.getElesByClass=function (strClassName,context){ if(typeof st ...
- Python Flask+Mysql练习题
#!/usr/bin/pythonfrom flask import Flask,render_template,request,redirect,sessionimport MySQLdb as m ...
- 编辑被标记为“只读”的Word文档
从邮件接收到的Word文档,打开时总是被标记为“只读”,在阅读时对其进行编辑,但不能保存,会提示文档为只读的.要想对其进行编辑并保存,需要进行一定的操作. 进入文件所在的目录,鼠标右键点击Word文档 ...
- php排序介绍_冒泡排序_选择排序法_插入排序法_快速排序法
这里我们介绍一些常用的排序方法,排序是一个程序员的基本功,所谓排序就是对一组数据,按照某个顺序排列的过程. 充效率看 冒泡排序法<选择排序法<插入排序法 排序分两大类: 内部排序法 交换式 ...