当前位置:网站首页>Valid bracketed strings [greedy exercise]
Valid bracketed strings [greedy exercise]
2022-07-30 17:11:00 【REN_Linsen】
前言
Greed is the most important test of analytical ability,and training flexibility,Often not linked to the classic dead knowledge point,Instead, look for greedy points,Then use code to realize your ideas.So don't be in a daze,Need to think steadily!
一、有效的括号字符串

二、Look for greedy points
package everyday.greed;
// 有效的括号字符串.
public class CheckValidString {
/* target:不要发呆!Think steadily. Use general stack thinking for bracket matching,碰到*号怎么办? 用数组+len实现栈,当碰到 ')' 时,take the handle ‘*’ 号前面 ‘(' 变为 ’*‘ ,为什么这样做? 试想一下,此时的 ’*‘ 可变 ’)‘ ,也可变 '(' ,也可当作 空串,This is when local information cannot make a correct decision! 如果缺'(',留住*Make it change after the number;If missing right,留着*Make it change after the number.Empty strings are the same. 贪心核心:把*留到最后,To prevent the wrong use in the early stage,After all it is omnipotent,But it is not omnipotent,Let the fixed brackets match first to cancel the left and right brackets. */
public boolean checkValidString(String s) {
int[] sk = new int[100];
int len = 0;
for (char c : s.toCharArray()) {
if (c == '(') sk[len++] = 1;
if (c == '*') sk[len++] = 0;
if (c == ')') {
if (len == 0) return false;
// Look for the front ’(‘ 号
int i = len - 1;
while (i >= 0 && sk[i] != 1) --i;
if (i >= 0) sk[i] = 0;// Change the left parenthesis to an asterisk.
len--;
}
}
// Get global information,It is time to make a correct judgment.
// 看看后面的 星号 Is it enough to fill the front 左括号.
int m = 0;// Represents the number of asterisks that follow.注:There are actually abstract ideas from previous exercises here,We don't care about this one number,We care how many symbols this group has,将数量m抽象出来.
for (int i = len - 1; i >= 0; i--) {
if (sk[i] == 1 && m == 0) return false;
if (sk[i] == 0) ++m;
else --m;
}
return true;
}
}
总结
1)Practice greed problems,Can exercise analytical skills,Skillful use of data structures.
2)不要发呆,Think steadily!
3)Train abstract skills,Otherwise, it is impossible to cleverly convert real-world problems into code,Or rather difficult to convert.转换问题(Match the data structure&算法&循环分支&抽象能力)It is one of the major directions to solve the problem,Another category is the high complexity,The ability to dismantle problems is required,Then contact the relevant knowledge points to combine.
4)总的来说,It's abstraction & Complex component pie,Corresponds to abstract ability & Knowledge point combination coordination ability.
参考文献
边栏推荐
猜你喜欢

Discuz magazine/news report template (jeavi_line) UTF8-GBK template

torch.optim.Adam() 函数用法

理解实现搜索二叉树
![[Geek Challenge 2020] Roamphp1-Welcome](/img/3b/2fa91f7478b8abf6efe0feafd24e58.png)
[Geek Challenge 2020] Roamphp1-Welcome

Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions

每日一题:两数之和

KDD 2020 | 深入浅出优势特征蒸馏在淘宝推荐中的应用

Tensorflow模型量化(Quantization)原理及其实现方法

Mongoose模块

.NET 6.0中使用Identity框架实现JWT身份认证与授权
随机推荐
What does a good resume look like in the eyes of a big factory interviewer?
华为无线设备Mesh配置命令
一篇文 带你搞懂,虚拟内存、内存分页、分段、段页式内存管理(超详细)
[MRCTF2020]Ezaudit
牛客网刷题——运算符问题
SLIM: Sparse Linear Methods (TopN推荐)
.NET 6.0中使用Identity框架实现JWT身份认证与授权
olap——入门ClickHouse
微信小程序picker滚动选择器使用详解
C# 连接SQL Sever 数据库与数据查询实例 数据仓库
每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
镜像站收集
data storage
OpenCV形状检测
向量检索基础方法总结
哎,这要人老命的缓存一致问题啊
PHP message feedback management system source code
LeetCode167:有序数组两数之和
SQLServer下载与安装
Microsoft Office 2019 软件下载安装详细教程!