暴力法超时:思想:动态规划

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. 将字符串翻转到单调递增的更多相关文章

  1. [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), ...

  2. [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), ...

  3. [LeetCode] Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  4. [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 ...

  5. 【LeetCode】738. 单调递增的数字

    738. 单调递增的数字 知识点:字符串:贪心 题目描述 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x ...

  6. Java实现 LeetCode 738 单调递增的数字(暴力)

    738. 单调递增的数字 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= ...

  7. 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹

    一,    最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...

  8. 【LCS,LIS】最长公共子序列、单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  9. nyoj 17 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

随机推荐

  1. shell脚本,如何用shell打印金字塔

  2. shell脚本,计算从0+2+4+6+....100的结果是多少?

    [root@localhost wyb]# cat evenjia.sh #!/bin/bash #从0++++...100的结果 i= ` do sum=$(($sum+i)) i=$(($i+)) ...

  3. 大数据学习系列之Hadoop、Spark学习线路(想入门大数据的童鞋,强烈推荐!)

    申明:本文出自:http://www.cnblogs.com/zlslch/p/5448857.html(该博客干货较多) 1 Java基础: 视频方面:          推荐<毕向东JAVA ...

  4. perl中foreach(一)

    perl中的foreach结构  首先语法 foreach $rock(qw /bedrock slate lava/){        $rock="\t$rock";      ...

  5. python上的数据库sqlite3——插入多行数据

    学校课程上的一个知识点,一个简单的课后习题:一劳永逸实现多行数据的插入(应该是这个意思,老师也没讲清楚).直接上代码了没啥好讲的,我感觉这个思路好捞. import sqlite3 con = sql ...

  6. LeetCode 637. Average of Levels in Binary Tree(层序遍历)

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  7. 【MySQL】Got fatal error 1236原因和解决方法

    一 前言  MySQL 的主从复制作为一项高可用特性,用于将主库的数据同步到从库,在维护主从复制数据库集群的时候,作为专职的MySQL DBA,笔者相信大多数人都会遇到“Got fatal error ...

  8. C#简易日志输出

    精简版: public static void WriteLog(string message, string group = "") { var logPath = System ...

  9. Nmap手册

    转自:http://drops.xmd5.com/static/drops/tips-4333.html 0x00:说明 只是一个快速查询手册,理论的东西都没有补充,欢迎大家积极在评论区补充自己常用的 ...

  10. 10大vim插件

    Taglist taglist是一个用于显示定位程序中各种符号的插件,例如宏定义.变量名.结构名.函数名这些东西 我们将其称之为符号(symbols),而在taglist中将其称之为tag.显然,要想 ...