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. ionic 安装本地插件极光推送

    问题:按照官方文档的步骤 假如把插件保存到了D:\push\jpush,当执行到 cordova plugin add D:\push\jpush 的时候,ionic 不是从本地目录安装,而是从reg ...

  2. [转]HttpURLConnection的使用

    /* * URL请求的类别分为二类,GET与POST请求.二者的区别在于: * a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, * b:) post与get ...

  3. 当BPM业务流程管理遇上制造业

    2015年5月,K2正式与赛科利签约,准备上线核心采购类流程,包括PR.PO.Payment.供应商管理等. 上海赛科利汽车模具技术应用有限公司隶属于上汽集团,现有员工2300余人.为解决汽车“安全” ...

  4. Scala深入浅出实战经典---001-Scala开发环境搭建和HelloWorld解析

    001-Scala开发环境搭建和HelloWorld解析 Scala 函数式编程和面向对象结合的语言 函数式编程擅长数值计算 面向对象擅长大型工程和项目的组织以及团队的分工协作 Scala会是下一个伟 ...

  5. AdaBoosting 3

    在学习AdaBoosting和online Boosting, 最好有bagging和boosting基础,这样看起来比较会比较顺.有空再补上. AdaBoost 算法的主要思想之一就是在训练集上维护 ...

  6. 北大poj-1088

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 88484   Accepted: 33177 Description ...

  7. BFC学习笔记

    BFC:块级格式化上下文 占用某一块的空间位置,主要用于清除内部浮动(防止因浮动脱离文档流引起的布局错乱),margin值的嵌套(之前写过一篇关于margin-top嵌套的解决方法),三列布局(占用空 ...

  8. 公测后,微信小程序应用可能被拒原因.

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 34.0px "PingFang SC Semibold"; color: #23232 ...

  9. JS运动基础(二) 摩擦运动、缓冲运动

    摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...

  10. MEDIA-SYSSERVICES媒体播放

    1 简单的音乐播放器 1.1 问题 本案例结合之前所学的网络和数据解析等知识完成一个网络音乐播放器,如图-1所示: 图-1 1.2 方案 首先创建一个SingleViewApplication应用,在 ...