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.

public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0)
return 0;
String[] dp = new String[s.length()];
dp[0] = s.substring(0, 1);
int index = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1))
dp[i] = s.substring(i - 1, i);
else {
index = dp[i - 1].indexOf(s.charAt(i));
if (index == -1)
dp[i] = dp[i - 1] + s.charAt(i);
else
dp[i] = dp[i - 1].substring(index+1) +s.charAt(i);
} }
index = 0;
for (int i = 0; i < s.length(); i++) {
if (dp[i].length() > index)
index = dp[i].length();
}
return index;
}
}

  

(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. java中怎么在table上显示数据

    连接oracle:String result = ""; // 查询结果字符串 String sql = "select * from test"; // SQ ...

  2. CAN基础知识

    CAN:Controller Area Network,是ISO国际标准化的串行通信协议. CAN控制器根据两根线上的电位来判断总线电平.总线电平分为显性电平和隐性电平,二者必居其一.发送方通过使总线 ...

  3. vs2008编译openssl问题

    运行openssl demo 时,debug 版本正常,release 版本报异常:OPENSSL_Uplink(585E6000,08): no OPENSSL_Applink .demo 编译环境 ...

  4. 理解OAuth 2.0(转载)

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标 ...

  5. Day19_IO第一天

    1.异常 1.概念      程序出现不正常的情况 2.异常体系(掌握)      Throwable           |-Error                               ...

  6. CentOS进程管理

    Linux系统中的基本运行单位是进程,通过对系统系统中的进程的管理能够对系统的实时运行状态进行了解和调度.Linux中提供了用于查看.调整和停止进程的命令.本文仍然以RHEL6说明Linux系统的进程 ...

  7. Linux命令--系统中常用的查看命令

    摘自 http://my.oschina.net/syyzhan/blog/277536 1.查看日志文件 使用命令:cat 或者 tail -f(默认查看文件尾部10行) 相关日志文件: /var/ ...

  8. codeforces105d Bag of mice ——概率DP

    Link: http://codeforces.com/problemset/problem/148/D Refer to: http://www.cnblogs.com/kuangbin/archi ...

  9. sscanf格式化输出

    char DesChar[20] = {0}; char* SouChar= "1cZCD23456abEFdedfB"; sscanf(SouChar,"%[^A-Z] ...

  10. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...