今天的字符类还比较简单

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的更多相关文章

  1. 验证码识别之w3cschool字符图片验证码(easy级别)

    起因: 最近在练习解析验证码,看到了这个网站的验证码比较简单,于是就拿来解析一下攒攒经验值,并无任何冒犯之意... 验证码所在网页: https://www.w3cschool.cn/checkmph ...

  2. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  3. LeetCode.942-DI字符串匹配(DI String Match)

    这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  5. Leetcode解题思路总结(Easy篇)

    终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...

  6. C#版 - Leetcode 65. 有效数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  7. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  8. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  9. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

随机推荐

  1. 前端零基础入门:页面结构层HTML

    HTML页面结构层HTML 分以下方面学习: HTML基础, HTML表格, HTML表单, 搭建网页结构 html基础 学习前端的第一门课程,为HTML基础,HTML标记语言,它是网页制作的第一步. ...

  2. GCC编译UDF和gdb调试UDF

    Fluent版本:19.0 前面我们介绍过使用VS来编译和调试UDF,其实我们也可以用GCC来编译UDF.gdb调试UDF.本次介绍的方法更具有通用性,也适用于Linux下Fluent的UDF编译和调 ...

  3. NOIP 2013货车运输

    当然这题有很多做法,但是我看到没有人写DSU的很惊奇 按照之前做连双向边题的经验,这题可以用并查集维护联通 然后对于每个询问\(x,y\),考虑启发式合并 当两个点集\(x,y\)合并时,一些涉及到其 ...

  4. python 项目实战之装饰器

    import logging def use_logging(func): def writelog(*args, **kwargs): logging.warning("%s is run ...

  5. 从太空到地球某个位置的轨迹录像制作 | Earth Zoom in/out Tutorial (Record Video)

    视频教程:Google Earth - Earth Zoom in/out Tutorial (Record Video) 下载google earth 在search里输入你想要去的地名 zoom ...

  6. 把ngnix注册为linux服务 将Nginx设置为linux下的服务 并设置nginx开机启动

    一.创建服务脚本 vim /etc/init.d/nginx 脚本内容如下 #! /bin/sh# chkconfig: - 85 15 PATH=/usr/local/nginx/sbin/ DES ...

  7. unable to access android sdk add-on list的解决

    第一次安装android studio时候弹出unable to access android sdk add-on list原因是你电脑没有SDK而且你下载的android studio又是不带SD ...

  8. mp4文件转码为m3u8

    https://bbs.csdn.net/topics/392046401 *********************************************** 转码完成,我直接播放m3u8 ...

  9. Ant Design Pro Vue 时间段查询 问题

    <a-form-item label="起止日期" :labelCol="{lg: {span: 7}, sm: {span: 7}}" :wrapper ...

  10. 【翻译】Flink Table Api & SQL — Hive —— 读写 Hive 表

    本文翻译自官网:Reading & Writing Hive Tables  https://ci.apache.org/projects/flink/flink-docs-release-1 ...