Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

  1. public class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. if (s.length() == 0)
  4. return 0;
  5. String[] dp = new String[s.length()];
  6. dp[0] = s.substring(0, 1);
  7. int index = 0;
  8. for (int i = 1; i < s.length(); i++) {
  9. if (s.charAt(i) == s.charAt(i - 1))
  10. dp[i] = s.substring(i - 1, i);
  11. else {
  12. index = dp[i - 1].indexOf(s.charAt(i));
  13. if (index == -1)
  14. dp[i] = dp[i - 1] + s.charAt(i);
  15. else
  16. dp[i] = dp[i - 1].substring(index+1) +s.charAt(i);
  17. }
  18.  
  19. }
  20. index = 0;
  21. for (int i = 0; i < s.length(); i++) {
  22. if (dp[i].length() > index)
  23. index = dp[i].length();
  24. }
  25. return index;
  26. }
  27. }

  

(DP)3.Longest Substring Without Repeating Characters的更多相关文章

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

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

  2. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  3. LeetCode[3] Longest Substring Without Repeating Characters

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

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

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

  5. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  6. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. 【leetcode】Longest Substring Without Repeating Characters

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

  8. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. leetcode: longest substring without repeating characters

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

随机推荐

  1. Android Studio 查看密钥库证书指纹SHA1

    打开DOC命令窗体

  2. System.exit(0)作用

    System.exit(0)作用   public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...

  3. [vijos P1035] 贪婪的送礼者

    为何我要做此等弱智题,只因我太久不码代码,心有所虚… 明天的任务是,做些难题,累了就理房间,实在不行就睡觉,不要做别的事情w 目测自己做不到呢OAO program vijos_p1035; ..] ...

  4. K2BPM怎么让金融数据更有意义?

    西南证券成立于1999年,是唯一一家注册地在重庆的全国综合性证券公司,也是中国第九家上市证券公司和重庆第一家上市金融机构.公司现有员工逾2000名,在全国27个省份获批设立109家证券营业部,拥有17 ...

  5. <li>高度自适应

    使用ul和li代替表格进行排版的时候,会发现li无法自适应高度. 只需要将li的overflow置为auto就可以了,因为li默认的overflow是visible,会将内部元素显示在li之外.   ...

  6. Android 4.2以上的手机USB调试设置

    今天遇到一个问题,我手上有两部手机一部是红米.一部是中兴的青漾QY N986,两部手机的Android系统都是4.2.1的,连接到电脑测试,找了半天没有找到设置开发者选项,后来在网上找了半天,才发现g ...

  7. .net 后台读取pdf的值

    在网上找了内容 下载了这个插件 引用在了项目中 然后找到pdf中的位置 进行读取 string pdfPath = Server.MapPath("~/ViewPatPdf.pdf" ...

  8. import和from import陷阱二

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 #from os import path import os.path path='/home/vamei/doc/file.txt' ...

  9. Linux上vi(vim)编辑器使用教程

    vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...

  10. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...