leetcode 340. Longest Substring with At Most K Distinct Characters
来源:互联网 发布:最新网络歌曲下载 编辑:程序博客网 时间:2024/05/16 09:06
leetcode 340. Longest Substring with At Most K Distinct Characters
下面的实现是很直观的,记下当前所包含的字符数,采用双指针进行移动
public class Solution {public static void main(String[] args){Solution s = new Solution();s.lengthOfLongestSubstringKDistinct("abacccc", 2);} public int lengthOfLongestSubstringKDistinct(String s, int k) { int size = s.length(); if(size<=k) return size; if(k==0) return 0; char[] chars = s.toCharArray(); int i=0,j=0; int max = 0;// HashSet<Character> bag = new HashSet<Character>(); HashMap<Character, Integer> bag = new HashMap<Character, Integer>(); // bag.add(chars[0]); bag.put(chars[0], 1); while(i<size&&j<size){// System.out.println(bag.size()); if(bag.size()<=k){ max = max > (j-i+1) ? max :(j-i+1);// System.out.println(max); j++; if(j<size) {// bag.add(chars[j]); if(bag.containsKey(chars[j])) bag.put(chars[j], bag.get(chars[j])+1); else bag.put(chars[j], 1); } if(j==size) break; }else{ i++; if(i<size) { // System.out.println(i); // System.out.println(j); bag.put(chars[i-1], bag.get(chars[i-1])-1); if(bag.get(chars[i-1])==0) bag.remove(chars[i-1]); } } } return max; }}
而下面这种解法,在i 自增的时候,不再是一步一步增,直接跳转到所有元素最右出现的位置的最小位置(有点绕,但是对的)
class Solution(object): def lengthOfLongestSubstringKDistinct(self, s, k): """ :type s: str :type k: int :rtype: int """ d = {} low, ret = 0, 0 for i, c in enumerate(s): d[c] = i if len(d) > k: low = min(d.values()) print(low) del d[s[low]] print(d) low += 1 ret = max(i - low + 1, ret) return ret
java 版本
import java.util.HashMap;import java.util.HashSet;import java.util.Map;public class Solution {public static void main(String[] args){Solution s = new Solution();s.lengthOfLongestSubstringKDistinct("abacccc", 2);} public int lengthOfLongestSubstringKDistinct(String s, int k) { int size = s.length(); if(size<=k) return size; if(k==0) return 0; char[] chars = s.toCharArray(); int i=0,low=0; int max = 0;// HashSet<Character> bag = new HashSet<Character>(); HashMap<Character, Integer> bag = new HashMap<Character, Integer>(); // bag.add(chars[0]); while(i<size){ bag.put(chars[i], i); if(bag.size()>k){ low = findmin(bag); bag.remove(chars[low]); low = low + 1; } max = max>(i-low+1)?max:(i-low+1); i+=1; } return max; } public int findmin(HashMap<Character, Integer> bag){ int min = Integer.MAX_VALUE; for(Map.Entry<Character, Integer> entry: bag.entrySet()){ if(entry.getValue()<min){ min = entry.getValue(); } } return min; } }
阅读全文
0 0
- LeetCode 340. Longest Substring with At Most K Distinct Characters
- leetcode 340. Longest Substring with At Most K Distinct Characters
- leetcode Longest Substring with At Most K Distinct Characters
- Leetcode Longest Substring with At Most K Distinct Characters
- [leetcode] 340. Longest Substring with At Most K Distinct Characters 解题报告
- LeetCode 340. Longest Substring with At Most K Distinct Characters(最长字串)
- Leetcode Everyday: 340. Longest Substring with At Most K Distinct Characters
- [Leetcode] 340. Longest Substring with At Most K Distinct Characters 解题报告
- lintcode:Longest Substring with At Most K Distinct Characters
- Longest Substring with At Most K Distinct Characters
- Longest Substring with At Most K Distinct Characters
- [Amazon] Longest Substring with At Most K Distinct Characters
- [Leetcode] Longest Substring with At Most Two Distinct Characters
- Leetcode Longest Substring with At Most Two Distinct Characters
- leetcode 159: Longest Substring with At Most Two Distinct Characters
- [Leetcode]Longest Substring with At Most Two Distinct Characters
- LeetCode - Longest Substring with At Most Two Distinct Characters
- Leetcode: Longest Substring with At Most Two Distinct Characters
- Springboot-系统异常统一处理(接口编程异常处理)
- 常用类的对比
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- nltk 中的 sents 和 words
- 5.jfinal上传下载文件
- leetcode 340. Longest Substring with At Most K Distinct Characters
- Java NIO Pipe使用示例
- Java集合分类以及各自特点
- jdk 源码分析(2)java hashtable的结构及hashMap对比
- 查看本机ssh公钥,生成公钥
- Redis学习——01.redis安装
- 链表操作(链表面试题)
- Bye, sent_keys
- MySQL存储过程中的3种循环