【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 ...
随机推荐
- 洛谷p1966火柴排队题解
ps:鉴于你们的蒟蒻yxj实在太蒻辽, 所以, 看不懂也是正常的........ 树状数组 xxy学姐给我们讲的树状数组, 她讲的真的是太好啦!qwq!吹爆xxy 然后, 为了巩固自己, 硬着头皮写题 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- 「NOIP2016」换教室
传送门 Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 $ 2n $ 节课程安排在 $ n $ 个时间段上.在第 $ i ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- unity:坐标变换 - 两个函数
在cocos中,我们知道有如下的坐标变换函数: CCPoint convertToNodeSpace(const CCPoint& worldPoint);CCPoint convertToW ...
- 在 Ubuntu/Debian 下安装 PHP7.3 教程
介绍 最近的 PHP 7.3.0 已经在 2018 年12月6日 发布 GA,大家已经可以开始第一时间体验新版本了,这里先放出 PHP7.3 安装的教程以便大家升级. 适用系统: Ubuntu 18. ...
- linux下安装 ping 命令
使用docker仓库下载的ubuntu 14.04 镜像.里面精简的连 ping 命令都没有.google 百度都搜索不到ping 命令在哪个包里. 努力找了半天,在一篇文章的字里行间发现了 ping ...
- pyqt(day1)
参考代码地址:https://github.com/cxinping/Pyqt5 pyqt在线帮助文档:https://www.riverbankcomputing.com/static/Docs/P ...
- git推送本地分支到远程仓库并在远程仓库创建新分支
$ git push <远程主机名> <本地分支名>:<远程分支名> git push master test:test #master 为设置的远程仓库别名,第一 ...
- php curl 转为 x-www-form-urlencoded 方式
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); fr ...