【LEETCODE】65、字符分类,medium&easy级别,题目:20、647、3
今天的字符类还比较简单
package y2019.Algorithm.str.easy; import java.util.HashMap;
import java.util.Map;
import java.util.Stack; /**
* @ClassName IsValid
* @Description 20. Valid Parentheses
*
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
*
* An input string is valid if:
*
* Open brackets must be closed by the same type of brackets.
* Open brackets must be closed in the correct order.
* Note that an empty string is also considered valid.
*
* @Author xiaof
* @Date 2019/8/4 15:46
* @Version 1.0
**/
public class IsValid { public boolean solution(String s) { Map comMap = new HashMap();
Stack stack = new Stack();
comMap.put('(', ')');comMap.put('[', ']');comMap.put('{', '}');
//1.遍历字符串,获取每一个字符
char cs[] = s.toCharArray();
for (int i = 0; i < cs.length; ++i) {
//2.判断是是否是:(,[,{中的字符,如果是那么就入栈,如果不是就出栈
if(comMap.containsKey(cs[i])) {
//如果key包含
stack.push(cs[i]);
} else { if(stack.size() <= 0) {
return false;
} //3.判断出栈的数据和当前的数据是否正好配对,如果是,那么就ok,如果不是,那么就false
char temp = (char) stack.pop();
if((char) comMap.get(temp) != cs[i]) {
//如果不等
return false;
}
}
} if(stack.size() > 0) {
return false;
} return true; } public static void main(String[] args) {
String s = "()"; IsValid fuc = new IsValid(); fuc.solution(s); } }
package y2019.Algorithm.str.medium; /**
* @ClassName CountSubstrings
* @Description 647. Palindromic Substrings
*
* Given a string, your task is to count how many palindromic substrings in this string.
*
* The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
*
* Example 1:
*
* Input: "abc"
* Output: 3
* Explanation: Three palindromic strings: "a", "b", "c".
*
*
* Example 2:
*
* Input: "aaa"
* Output: 6
* Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
*
* 统计字符中是否包含回文字符的字串
*
* @Author xiaof
* @Date 2019/8/4 16:30
* @Version 1.0
**/
public class CountSubstrings { public int solution(String s) {
//双层循环遍历所有字串
int count = 0;
char[] source = s.toCharArray();
for (int i = 0; i < source.length; ++i) {
for (int j = i; j < source.length; ++j) {
//遍历所有的字符串
if(isPalindromic(i, j, source)) {
count++;
}
}
} return count;
} /**
*
* @param l 左边索引
* @param r 右边索引
* @param source 原始字符的字符数组
* @return
*/
public boolean isPalindromic(int l, int r, char[] source) {
while (l <= r) {
if (source[l] == source[r]) {
++l;
--r;
} else {
return false;
}
} return true;
} }
package y2019.Algorithm.str.medium; import java.util.HashMap;
import java.util.Map; /**
* @ClassName LengthOfLongestSubstring
* @Description 3. Longest Substring Without Repeating Characters
*
* Given a string, find the length of the longest substring without repeating characters.
*
* Example 1:
*
* Input: "abcabcbb"
* Output: 3
* Explanation: The answer is "abc", with the length of 3.
* Example 2:
*
* Input: "bbbbb"
* Output: 1
* Explanation: The answer is "b", with the length of 1.
* Example 3:
*
* Input: "pwwkew"
* Output: 3
* Explanation: The answer is "wke", with the length of 3.
* Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*
* @Author xiaof
* @Date 2019/8/4 17:30
* @Version 1.0
**/
public class LengthOfLongestSubstring { public int solution(String s) { if(s == null || s.equals("")) {
return 0;
} //统计最长连续子字串,那么我们只需要每次剔除重复的那个字符,然后从那个位置开始就可以了
int start = 0, count = 1;
Map num = new HashMap();
boolean lastcount = false;
char[] source = s.toCharArray();
num.put(source[0], 0); for (int i = 1; i < source.length; ++i) {
//判断前面的字串中是否有包含,这里要有(int) num.get(source[i]) >= start,而且是大于等于,避免之前跳过的数据干扰,并且不能排除掉起始位置
if (num.containsKey(source[i]) && (int) num.get(source[i]) >= start) {
//如果包含了,说明之前已经出现重复的字串,那么统计一波
count = Math.max(count, (i - start));
start = (int) num.get(source[i]) + 1;
num.put(source[i], i);
} else {
num.put(source[i], i);
if (i == source.length - 1) {
lastcount = true;
}
}
} //循环到最后,计算最后一个位置
if (lastcount) {
count = Math.max(count, (source.length - start)); } return count; } public static void main(String[] args) {
String s = "pwwkew";
String s1 = "tmmzuxt";
String s2 = "abcabcbb"; LengthOfLongestSubstring fuc = new LengthOfLongestSubstring(); fuc.solution(s2); }
}
【LEETCODE】65、字符分类,medium&easy级别,题目:20、647、3的更多相关文章
- 验证码识别之w3cschool字符图片验证码(easy级别)
起因: 最近在练习解析验证码,看到了这个网站的验证码比较简单,于是就拿来解析一下攒攒经验值,并无任何冒犯之意... 验证码所在网页: https://www.w3cschool.cn/checkmph ...
- LeetCode:颜色分类【75】
LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...
- LeetCode.942-DI字符串匹配(DI String Match)
这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- Leetcode解题思路总结(Easy篇)
终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...
- C#版 - Leetcode 65. 有效数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
随机推荐
- 同余方程组(EXCRT)(luogu4777)
#include<cstdio> #include<algorithm> #define ll long long using namespace std; ll k; ll ...
- python变量d的说明
[变量] 什么是变量: 变:现实世界中的状态是会发生改变的. 量:记录现实世界中的状态,让计算机能够像人一样去识别世间万物 是变化的量 变量的组成: 变量名:变量名用来引用变量值,但凡需要用变量值,都 ...
- JavaScript 整数转大写中文
function toChinese(money){ var chNum=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']; var maxnum=999999999 ...
- starUML
下载地址: https://www.qqxiazai.com/down/10296.html 下载后解压,先运行 绿化.exe 然后右键管理员运行 StarUML.exe 进入后就可以画UML以及时序 ...
- 倍增&矩阵乘法 专题复习
倍增&矩阵乘法 专题复习 PreWords 这两个基础算法我就不多说啦,但是还是要介绍一下" 广义矩阵 "乘法 其实就是把矩阵换成取\(max\),然后都一样... 据神仙 ...
- Dubbo自定义Filter统一处理异常
Dubbo版本:2.7 使用自定义Filter注意事项 1.自定义名称不能和默认Filter相同,否则可能不生效 2.只用定义Filter类和META-INF下的文本文件,不用添加配置,@Activa ...
- Java NIO 文件通道使用
读取一个文件的内容,然后写入另外一个文件 public class NioTest4 { public static void main(String[] args) throws Exception ...
- httpcomponents 发送get post请求
引入的包为: <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <de ...
- kotlin基础 空值检查
NULL检查机制 Kotlin的空安全设计对于声明可为空的参数,在使用时要进行空判断处理,有两种处理方式,字段后加!!像Java一样抛出空异常,另一种字段后加?可不做处理返回值为 null或配合?:做 ...
- Java13新特性 -- switch表达式动态CDS档案(动态类数据共享归档)
支持在Java application执行之后进行动态archive.存档类将包括默认的基础层CDS存档中不存在的所有已加载的应用程序和库类.也就是说,在Java 13中再使用AppCDS的时候,就不 ...