【LEETCODE】66、字符串分类,hard级别,题目:32,72,76
package y2019.Algorithm.str.hard; import java.util.Stack; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.str.hard
* @ClassName: LongestValidParentheses
* @Author: xiaof
* @Description: 32. Longest Valid Parentheses
*
* Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
*
* Example 1:
*
* Input: "(()"
* Output: 2
* Explanation: The longest valid parentheses substring is "()"
* Example 2:
*
* Input: ")()())"
* Output: 4
* Explanation: The longest valid parentheses substring is "()()"
*
* 求最长有效括号对,那么就是要确保子串是连续
* @Date: 2019/8/6 9:51
* @Version: 1.0
*/
public class LongestValidParentheses { public int solution(String s) {
//用来统计最大的括号数,我们只要能匹配成功就行
Stack<Integer> stack = new Stack();
char[] ss = s.toCharArray();
int n = ss.length, res = 0; //遍历
for (int i = 0; i < ss.length; ++i) {
if (ss[i] == '(') {
stack.push(i); //用来存放int位置
} else {
if (!stack.isEmpty()) {
//如果不为空,那么就可以进行匹配
if (ss[stack.peek()] == '(') stack.pop();
else stack.push(i);
} else {
stack.push(i);
}
}
}
//我们只需要统计剩下无法匹配的连续的(的数据比较就可以了
//如果栈为空,那么正好完全匹配
if (stack.isEmpty()) {
res = n;
} else {
//如果不是完全匹配,那么就要计算连续两个(的间隔
int r = n, l = 0;
while (!stack.isEmpty()) {
//不断获取最后的结束符号位置
l = stack.pop();
//取最长的串位置
res = Math.max(r - l - 1, res);
r = l; //更新位置
}
//最后还要判断从起始位置开始到当前位置消掉的长度
res = Math.max(r, res);
}
return res;
}
}
package y2019.Algorithm.str.hard; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.str.hard
* @ClassName: MinDistance
* @Author: xiaof
* @Description: 72. Edit Distance
* Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
*
* You have the following 3 operations permitted on a word:
*
* Insert a character
* Delete a character
* Replace a character
* Example 1:
*
* Input: word1 = "horse", word2 = "ros"
* Output: 3
* Explanation:
* horse -> rorse (replace 'h' with 'r')
* rorse -> rose (remove 'r')
* rose -> ros (remove 'e')
* Example 2:
*
* Input: word1 = "intention", word2 = "execution"
* Output: 5
* Explanation:
* intention -> inention (remove 't')
* inention -> enention (replace 'i' with 'e')
* enention -> exention (replace 'n' with 'x')
* exention -> exection (replace 'n' with 'c')
* exection -> execution (insert 'u')
* @Date: 2019/8/7 11:42
* @Version: 1.0
*/
public class MinDistance { public int solution(String word1, String word2) {
int w1 = word1.length(), w2 = word2.length();
char w1s[] = word1.toCharArray(), w2s[] = word2.toCharArray();
int[][] dpcost = new int[w1 + 1][w2 + 1]; //初始化
for (int i = 0; i <= w1; ++i) {
dpcost[i][0] = i; //标识要把word1的i个字符化为0个,那就删i次
}
for (int j = 0; j <= w2; ++j) {
dpcost[0][j] = j;
}
//这题动态规划了,比较复杂说实话 //遍历
for (int i = 1; i < dpcost.length; ++i) {
for (int j = 1; j < dpcost[i].length; ++j) {
//我们把动态规划递增的量设置为,word1前i个字符,转换为word2前j个字符需要的最小操作数
//f(i,j) = f(i-1, j-1) 当word1[i] == word2[j]的时候,相等字符,那么当前字符就不需要花费操作次数
if (w1s[i - 1] == w2s[j - 1]) {
dpcost[i][j] = dpcost[i - 1][j - 1];
} else {
//当word1[i] != word2[j]的时候 分三种情况平衡
//1.进行插入操作, 那就是新增一个一样的字符和word2进行匹配,那就是word2的j位就直接新增进去,就不用比较了,我们取没有j号的次数
// f(i,j) = f(i, j-1) + 1;
int insert = dpcost[i][j - 1];
//2.当进行删除操作,那就是把word1当前字符删除掉,当前位置相当于不比较
// f(i,j)=f(i-1,j) + 1;
int remove = dpcost[i - 1][j];
//3.当进行替换操作,那就是把当前字符完全替换,那么就是新增一个操作,其余和之前一样
// f(i,j)=f(i-1,j-1) + 1;
int replace = dpcost[i - 1][j - 1]; dpcost[i][j] = Math.min(insert, Math.min(remove, replace)) + 1;
}
}
} return dpcost[w1][w2]; }
}
package y2019.Algorithm.str.hard; import java.util.HashMap;
import java.util.Map; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.str.hard
* @ClassName: MinWindow
* @Author: xiaof
* @Description: 76. Minimum Window Substring
* Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
*
* Example:
*
* Input: S = "ADOBECODEBANC", T = "ABC"
* Output: "BANC"
* Note:
*
* If there is no such window in S that covers all characters in T, return the empty string "".
* If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
* @Date: 2019/8/7 15:15
* @Version: 1.0
*/
public class MinWindow { public String solution(String s, String t) {
if (s == null || t == null || s.length() < t.length() || s.length() == 0 || t.length() == 0) {
return "";
}
//用索引,并且是单次循环
int l = 0, r = 0, count = t.length(), minle = s.length(), minl = 0, minr = 0;
Map<Character, Integer> map = new HashMap();
boolean pipeiok = false;
//第一次循环,初始化map
for (int i = 0; i < t.length(); ++i) {
map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) + 1);
} //循环遍历字符串
while (r < s.length()) {
//计算是否存在所有字符的子串
char curc = s.charAt(r);
if (map.containsKey(curc)) {
//如果包含
map.put(curc, map.get(curc) - 1);
if (map.get(curc) >= 0) {
//如果有效匹配
--count;
}
} //如果全部匹配完成,存在单字符匹配
while (count == 0 && l <= r) {
pipeiok = true;
int curlen = r - l + 1;
if (curlen <= minle) {
//存在更小值
minl = l;
minr = r;
minle = curlen;
} //更新最左边索引
char leftc = s.charAt(l);
if (map.containsKey(leftc)) {
//如果包含,那么就找到了最左边的第一个字符
//我们把这个位置的字符剔除掉,然后看是否可以找到更小的子串
map.put(leftc, map.get(leftc) + 1);
if (map.get(leftc) >= 1) {
//加一之后恢复到正常状态
count++;
}
}
//吧最左边右移一次
l++;
}
r++; //往后遍历
} return pipeiok == true ? s.substring(minl, minr + 1) : "";
} public static void main(String[] args) {
String s[] = {"ADOBECODEBANC", "ABC"};
String s1[] = {"aa", "aa"}; MinWindow fuc = new MinWindow(); fuc.solution(s1[0], s1[1]); }
}
【LEETCODE】66、字符串分类,hard级别,题目:32,72,76的更多相关文章
- 前端与算法 leetcode 8. 字符串转换整数 (atoi)
目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...
- Leetcode中字符串总结
本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode:字符串的排列【567】
LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...
- 前端与算法 leetcode 387. 字符串中的第一个唯一字符
目录 # 前端与算法 leetcode 387. 字符串中的第一个唯一字符 题目描述 概要 提示 解析 解法一:双循环 解法二:Set法单循环 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...
- 前端与算法 leetcode 66. 加一
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...
- LeetCode:字符串相加【415】
LeetCode:字符串相加[415] 题目描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100.num1 和num2 都只 ...
- PTA数据结构与算法题目集(中文) 7-6
PTA数据结构与算法题目集(中文) 7-6 7-6 列出连通集 (25 分) 给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时, ...
- 2017-3-7 leetcode 66 119 121
今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...
随机推荐
- 常用方法 DataTable转换为Entitys
备注:摘自网上 有附地址 public static List<T> DataTableToEntities<T>(this DataTable dt) where T : c ...
- A@GC*014
A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...
- Promise对异步编程的贡献以及基本API了解
异步: 核心: 现在运行的部分和将来运行的部分之间的关系 常用方案: 从现在到将来的等待,通常使用一个回调函数在结果返回时得到结果 控制台(因为console族是由宿主环境即游览器实现的)可能会使用异 ...
- Java程序设计学习知识点总结
Java程序设计学习知识点总结 Java语言简单,面向对象,分布式,解释性,健壮,安全与系统无关,可移植,高性能,多线程,动态语言. 什么是框架 可以认为是某种应用的半成品,就是一组组件用来完善自己的 ...
- GitHub 手把手教你如何把本地项目或代码提交到Github托管
GitHub 手把手教你如何把项目或代码提交到Github托管 启动Git Bash命令行 重点内容 1.首先打开你的github,点击新建项目,点击new repositories ,然后直接给项目 ...
- 下拉选择的blur和click事件冲突了
当写个下拉选择框时我们希望当input失去焦点时,下拉框消失,或者当选择下拉框中的内容的同时将内容填入input并且使下拉框消失. 这时候我们会想到blur和click,单独使用的时候是没有问题的,但 ...
- xargs 命令教程
转自阮一峰 http://www.ruanyifeng.com/blog/2019/08/xargs-tutorial.html 仅供个人交流学习 xargs是 Unix 系统的一个很有用的命令,但是 ...
- Android Studio 之 Activity 的生命周期
翻转屏幕,会重新加载Activity package com.example.activitylivecycle; import android.os.Bundle; import android.u ...
- 【Beta阶段】第九次Scrum Meeting
每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #66 修改登录注册按钮https://github.com/rRetr0Git/rateMyCourse/issues/66 #69 后端 ...
- jdk 6-13最有价值新特性总结
355: Text Blocks (Preview) JDK 13的特性.简化了大段文本的换行,例如sql或xml段. Shenandoah GC. jdk 12作为实验特性引入. JEP330-启动 ...