1. import java.util.HashSet;
  2. import java.util.Set;
  3. /**
  4. * Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
  5. *
  6. * Created by lverpeng on 2017/6/26.
  7. *
  8. * Given a string, find the length of the longest substring without repeating characters.
  9. * For example, the longest substring without repeating letters for "abcabcbb" is "abc",
  10. * which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
  11. *
  12. */
  13. public class LongestSubstring {
  14. /**
  15. * 两个指针:一个指向当前字符,一个指向当前子串开始的位置
  16. * 将指向当前子串的指针依次向后移动,判断当前哈希表(用字符作为key,value不重要,可以直接用set)中是否存在当前字符
  17. * 如果已经存在,说明找到一个子串
  18. * 将指向当前子串的指针向后移动一个位置,作为新的子串起始位置
  19. * 计算刚刚结束子串的长度,和之前长度作比较,选出较大的一个
  20. * 如果不存在,更新当前子串的长度,加1
  21. * 将当前字符加入hash表,将当前指针向后移动,重复上面的操作
  22. *
  23. *
  24. *
  25. * @param str
  26. * @return
  27. */
  28. public int searchLongestSubstring (String str) {
  29. int lastIndext = 0;
  30. int maxLen = 0;
  31. Set<Character> charSet = new HashSet<Character>();
  32. for (int i = 0; i < str.length(); i++) {
  33. if (charSet.contains(str.charAt(i))) {
  34. int newLen = i - lastIndext;
  35. maxLen = Math.max(newLen, maxLen);
  36. lastIndext ++;
  37. } else {
  38. maxLen ++;
  39. }
  40. charSet.add(str.charAt(i));
  41. }
  42. return maxLen;
  43. }
  44. public static void main(String[] args) {
  45. LongestSubstring longestSubstring = new LongestSubstring();
  46. System.out.println(longestSubstring.searchLongestSubstring("abcabcbb"));
  47. }
  48. }

leetcode — longest-substring-without-repeating-characters的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  5. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  8. LeetCode——Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. [Leetcode] Longest Substring Without Repeating Characters (C++)

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  10. [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 20172325 2018-2019-2 《Java程序设计》第九周学习总结

    20172325 2018-2019-2 <Java程序设计>第九周学习总结 教材学习内容总结 图的定义 图是由顶点集(VertexSet)和边集(EdgeSet)组成,针对图G,顶点集和 ...

  2. solr7.7.0搜索引擎使用(三)(添加文件索引)

    众所周知,solr与es的最大区别是,solr可以对pdf,txt,doc等文件生成索引 那我们如何添加文件索引呢? 步骤1.添加core,取名暂且为 coreFile 在bin下执行命令 ./sol ...

  3. Linux 6上使用UDEV绑定共享存储

    1.硬盘的查看方式 [root@cl6-11gr2-rac1 ~]# ls -ltr /dev/sd* brw-rw----. 1 root disk 8, 48 8月 16 13:34 /dev/s ...

  4. C# Winform 登录中的忘记密码及自动登录

    本地保存登录账号实现忘记密码及自动登录 #region 删除本地自动登录及记住密码信息 /// <summary> /// 删除本地自动登录及记住密码信息 /// </summary ...

  5. GBDT(MART) 迭代决策树详解

    在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Re ...

  6. Linux 第五天

    网络命令 1)write 给在线用户发信息(需按Crtl+D保存结束,w命令可看在线用户) 语法:write 用户名 2)wall 发广播信息 英文原意:write all 语法:wall 信息 3) ...

  7. Memcache cpu占用过高

    分析应该是memcache的内存大小还是默认配置,已经满足不了当前的大数据量的需要了,大量的新缓存需要进入,同时大量的旧缓存又需要被淘汰出来,一进一出导致CPU占用过多.进入注册表,找到:HKEY_L ...

  8. SpringMVC源码解读 - HandlerMapping - RequestMappingHandlerMapping请求分发

    AbstractHandlerMethodMapping实现接口getHandlerInternal,定义查找流程 RequestMappingInfoHandlerMapping根据RequestM ...

  9. Codeforces828 D. High Load

    D. High Load time limit per test 2 seconds memory limit per test 512 megabytes input standard input ...

  10. 几种String对象方法的区别

    1.在String对象方法中,发现.slice()方法和.substring()方法的作用几乎相同,都是根据起始索引返回截取得到的字符串.经过查阅资料和实测得到区别: 正常情况下索引都为正值,返回值为 ...