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 输入 ...
随机推荐
- hihoCoder-1098-kruskal
如果起始点和终止点的父节点相同,就说明它们就已经在同一个连通分量里面,说明,起始点和终止点在此之前就已经被连入同一个分量之中,如果此时还将起始点和终止点连入此分量,就会形成回路,想象一个三角形,你大概 ...
- struts2命名空间与访问路径
比如项目deom的struts.xml中有如下片段 Java代码 <package name="demo" extends="struts-default" ...
- 如何用纯 CSS 创作一个均衡器 loader 动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/oybWBy 可交互视频教 ...
- (转)UIPanGestureRecognizer
UIPanGestureRecognizer是UIGestureRecognizer类的一个扩展类,其扩展类有UITapGestureRecognizer,UIPinchGestureRecogniz ...
- Android 8.0 adb shell dumpsys activity activities | findstr mFocusedActivity 获取当前的 activity 显示空的
adb shell dumpsys activity activities | findstr mFocusedActivity Android 7.0 现象: Android 8.0 现象: 改用: ...
- Etree方式解析xml知识积累
movies.xml: <collection shelf="New Arrivals"> <movie title="Enemy Behind&quo ...
- Java 正则表达式详解---https://www.jb51.net/article/16829.htm
一.正则表达式基础知识 我们先从简单的开始.假设你要搜索一个包含字符“cat”的字符串,搜索用的正则表达式就是“cat”.如果搜索对大小写不敏感,单词“catalog”.“Catherine”.“so ...
- Java异常架构图及面试题---https://www.cnblogs.com/gaoweixiao99/p/4905860.html
https://www.cnblogs.com/gaoweixiao99/p/4905860.html 红色为检查异常,就是eclipse要提示你是try catch 还是throws. 非检查异常, ...
- 图论trainning-part-1 F. Highways
F. Highways Time Limit: 1000ms Memory Limit: 10000KB 64-bit integer IO format: %lld Java class ...
- 基于深度学习的目标检测技术演进:R-CNN、Fast R-CNN,Faster R-CNN
基于深度学习的目标检测技术演进:R-CNN.Fast R-CNN,Faster R-CNN object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.obj ...