【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 ...
随机推荐
- [转]Reids配置文件redis.conf中文详解
转自: Reids配置文件redis.conf中文详解 redis的各种配置都是在redis.conf文件中进行配置的. 有关其每项配置的中文详细解释如下: 对应的中文版解释redis.conf # ...
- 8-网页,网站,微信公众号基础入门(使用Python程序实现微信token验证)
https://www.cnblogs.com/yangfengwu/p/11062034.html 设置一下IP白名单: 填写自己的服务器的IP地址 是不是不可以哈,有多少人都会入这个坑.... ...
- 【CSGRound2】逐梦者的初心(洛谷11月月赛 II & CSG Round 2 T3)
题目描述# 给你一个长度为\(n\)的字符串\(S\). 有\(m\)个操作,保证\(m≤n\). 你还有一个字符串\(T\),刚开始为空. 共有两种操作. 第一种操作: 在字符串\(T\)的末尾加上 ...
- VxLAN原理
VxLAN 背景介绍: 从上个世纪虚拟化技术就被提出,但由于硬件技术达不到,而没能被重视,自本世纪初硬件制造技术越来越来强,导致很多单台物理机只跑一个应用或几个应用根本无法完全使用硬件的全部性能,导致 ...
- shell中的字符串操作——字符串的切割
default.yaml {default_baseurl: 'http://10.113.10.68:8082'} test.sh a=`cat default.yaml` t=":&qu ...
- Spring不能直接@autowired注入Static变量
一.业务场景 spring框架应用中有些静态方法需要依赖被容器管理的类,就像这样: @Component public class Test { @Autowired private static U ...
- FZU Monthly-201909 tutorial
FZU Monthly-201909 tutorial 题目(难度递增) easy easy-medium medium medium-hard hard 思维难度 AB CD EF G H A. I ...
- 冰多多团队-第三次Scrum会议
冰多多团队-第三次Scrum会议 会议基本情况 会议时间:4月9日 21:30 - 21:45 会议地点:新主楼F座2楼沙发休息处 工作情况 团队成员 已完成任务 待完成任务 zpj 接入Action ...
- OpenCV3与深度学习实例:Dlib+VGG Face实现两张脸部图像相似度比较
原文:https://my.oschina.net/wujux/blog/2221444 实现思路: 1.使用Dlib识别并提取脸部图像 2.使用VGG Face模型提取脸部特征 3.使用余弦相似度算 ...
- 使用 Microsoft.Web.Administration 管理iis
How to Automate IIS 7 Configuration with .NET How to Automate IIS 7 Configuration with .NET Are you ...