标签:循环 not rect toc find span key == index
今天的字符类还比较简单
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
标签:循环 not rect toc find span key == index
原文地址:https://www.cnblogs.com/cutter-point/p/11299060.html