leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)
题目
https://leetcode.com/problems/flip-string-to-monotone-increasing/
题解
方法1:前缀和
class Solution {public int minFlipsMonoIncr(String s) {int L = s.length();// i位置左边1的个数int[] leftOne = new int[L];for (int i = 0; i < L - 1; i++) {if (s.charAt(i) == '1') leftOne[i + 1] = leftOne[i] + 1;else leftOne[i + 1] = leftOne[i];}// i位置右边0的个数int[] rightZero = new int[L];for (int i = L - 1; i > 0; i--) {if (s.charAt(i) == '0') rightZero[i - 1] = rightZero[i] + 1;else rightZero[i - 1] = rightZero[i];}// 将i左边全变成1,i右边全变成0,总共需要改的个数int minSum = Integer.MAX_VALUE;for (int i = 0; i < L; i++) {minSum = Math.min(minSum, leftOne[i] + rightZero[i]);}return minSum;}
}
方法2:dp
int minFlipsMonoIncr(char *s) {int z = *s == '0' ? 0 : 1;int o = *s == '0' ? 1 : 0;for (s++; *s; s++){if (*s == '0'){o = (z < o ? z : o) + 1;}else{o = (z < o ? z : o);z++;}}return z < o ? z : o;
}
leetcode 926. Flip String to Monotone Increasing | 926. 将字符串翻转到单调递增(前缀和)相关推荐
- LeetCode 926. 将字符串翻转到单调递增(动态规划)
文章目录 1. 题目 2. 解题 1. 题目 如果一个由 '0' 和 '1' 组成的字符串,是以一些 '0'(可能没有 '0')后面跟着一些 '1'(也可能没有 '1')的形式组成的,那么该字符串是单 ...
- Leetcode 738. Monotone Increasing Digits
原题链接:https://leetcode.com/problems/monotone-increasing-digits/description/ 描述: Given a non-negative ...
- leetcode 1239. Maximum Length of a Concatenated String with Unique Characters | 1239. 串联字符串的最大长度(回溯)
题目 https://leetcode.com/problems/contiguous-array/ 题解 回溯法,每个字符串都有可能加或者不加. class Solution {int N;publ ...
- LeetCode算法入门- String to Integer (atoi)-day7
LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...
- 【To Understand !!! DP or 递归】LeetCode 87. Scramble String
LeetCode 87. Scramble String 参考链接:[1]https://blog.csdn.net/makuiyu/article/details/44926439 [2]http: ...
- 【重点!DP】LeetCode 97. Interleaving String
LeetCode 97. Interleaving String 参考网址:[1]https://www.youtube.com/watch?v=HmAF9xeS_2I [2]http://www.c ...
- leetcode 971. Flip Binary Tree To Match Preorder Traversal
leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...
- [LeetCode]844. Backspace String Compare 解题报告(C++)
[LeetCode]844. Backspace String Compare 解题报告(C++) 题目描述 Given two strings S and T, return if they are ...
- 738. Monotone Increasing Digits
内容: Given a non-negative integer N, find the largest number that is less than or equal to N with mon ...
最新文章
- ROS2概述和实践入门
- VMware下redhat9.0的上网设置
- 神奇的bibtex插件:你的论文文献小助手
- python web自动化测试实验报告_Python:web自动化测试
- iframe-父子-兄弟页面相互传值(jq和js两种方法)
- android接支付宝授权和支付功能
- java keytool用法,java中Keytool的使用总结
- 双线性变换(Tustin transform/bilinear transformation)
- 基于Java毕业设计在线商城系统源码+系统+mysql+lw文档+部署软件
- python通过selenium模块模拟自动登录(以QQ空间为例)
- 两组数据的偏差率_析数据 察得失 明对策 促提高 —记东升一中2021级月考二成绩分析会...
- 塑料齿轮模具设计指南
- java 逃逸分析_JVM之逃逸分析
- php 模板对象,php面向对象--PHP模板 ppt
- unity使用Sprite Editor图片切割功能减少性能损耗
- 最新------如何不通过AppStore,直接安装IOS应用程序 兼容最新版IOS系统
- 【微信H5】分享出去是链接,不是卡片的原因及解决方案
- 【罗塞塔石碑】—My Lover(One.iso)
- 从一台 Windows 10 上共享文件夹到Docker中的Volume卷
- C语言——十进制转化为二进制