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 输入 ...
随机推荐
- JAVA web项目转客户端(nativefier)
1.环境:windows 2.下载node.js 3.安装mode.js;记住安装目录 4.命令行进入安装目录 5.执行语句: npm install nativefier –g 进行安装 6.新建空 ...
- OC各种遍历方法的效率比较
看了一篇博客,挺有意思,OC各种遍历方法的效率,打算自己也测试一番.看看,究竟哪一个的效率更好一些! 准备工作:懒加载一个数组,创建一千万个对象添加到数组. #pragma mark - Lazy M ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- JavaScript调试技巧之console.log()详解--2015-08-07
对于JavaScript程序的调试,相比于alert(),使用console.log()是一种更好的方式,原因在于:alert()函数会阻断 JavaScript程序的执行,从而造成副作用:而cons ...
- url地址数据参数转化JSON对象(js三种方法实现)
当我们用get方法提交表单时,在url上会显示出请求的参数组成的字符串,例如:http://localhost:3000/index.html?phone=12345678901&pwd=12 ...
- sublime text 3143 最新激活方法
1)输入激活码 —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60 ...
- fopen()和socket()的内在联系
int portone=socket(AF_INET,SOCK_STREAM, 0); printf("portone=%d",portone); printf("ope ...
- UVA-1220-Party at Hali-Bula && UVA-1218-Perfect Service(树形DP)
UVA-1220-Party at Hali-Bula 题意: 一个公司员工要举行聚会,要求任意一个人不能和他的直接上司同时到场,一个员工只有一个支系上司,现在求最多有多少人到场,并且方案是否唯一(紫 ...
- HDU-1312-Black and Red
这题其实和POJ的1979是同一道题,当时POJ使用cin写的,所以读入的时候,就很正确. 这次用scanf读入的时候,就出现了问题,我们在读完宽高之后,要用getchar吸收掉回车,然后每行末尾的回 ...
- perl学习之子例程
1.system function && user function system fucntion:chomp reverse print... user function: & ...