今天的字符类还比较简单

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. 洛谷 P2356 【弹珠游戏】题解

    自我感觉应该没有用结构体做的吧 这道题其实非常水 很适合初学贪心的同学做一下 我好像没有用贪心做,嘻嘻 首先先读题, 题目中说这个游戏只能消灭当前所在位置的行.列的敌人 首先特判一下: if(tt== ...

  2. 数据结构之LinkList

    1.结构: 2.Link代码: public class Link { public int iData; public double dData; public Link next; public ...

  3. 【Beta】Scrum meeting 3

    目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit记录截图 小程序前端仓库 技术博客 写在前面 例会时间:5.7 22:30-23:00 例会地点:微信群语音 ...

  4. 深度排序模型概述(一)Wide&Deep/xDeepFM

    本文记录几个在广告和推荐里面rank阶段常用的模型.广告领域机器学习问题的输入其实很大程度了影响了模型的选择,因为输入一般维度非常高,稀疏,同时包含连续性特征和离散型特征.模型即使到现在DeepFM类 ...

  5. IIS 7中添加匿名访问FTP站点

    1. 开启FTP和IIS服务: 2.打开IIS 管理器: 我电脑上是IIS 7.5 ,所以选择第一个并点击打开哦. 如果你想知道自己IIS的版本,打开帮助菜单: 3. 新建FTP站点: 4. 填写站点 ...

  6. C# 序列化与反序列化Serialization之Json Xml Binary Soap JavaScript序列化

    所谓的序列化其实就是把一个内存中的对象信息转化成一个可以持久化保存的形式,方便保存数据库和文件或着用于传输, 序列化的主要作用是不同平台之间进行通信与信息的传递保存等,常用的有序列化有Json Xml ...

  7. Java: Java终止线程的几种方式

    首先说明,使用stop方法终止的方式已经在很久之前就被废弃了,在加锁的情况下有可能会造成死锁,这里不做讨论. 1. 使用标志位终止线程 在run()方法执行完毕后,该线程就终止了.但是在某些特殊的情况 ...

  8. 使用CompletableFuture实现业务服务的异步调用实战代码

    假如我有一个订单相关的统计接口,需要返回3样数据:今日订单数.今日交易额.总交易额. 一般的我们的做法是串行调用3个函数,把调用返回的结果返回给调用者,这3次调用时串行执行的,如果每个调用耗时1秒的话 ...

  9. sql 连续分组判断 partition by

    partition by 会根据分类字段进行排序 加上rownum 可以形成 每组从1开始重新排序 举个例子, 我要根据时间为依据,连续出现合并为一组,统计每组在区间里的次数 ------------ ...

  10. Nginx请求转发

    1.比如说我要将127.0.0.1/topics上的所有请求转发到xxx:xxx/上 修改 sudo vim /etc/nginx/nginx.conf server { listen 80; ser ...