【题目描述】

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is .

Given "bbbbb", the answer is "b", with the length of .

Given "pwwkew", the answer is "wke", with the length of . Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

【解决思路】

方法可能比较笨,但是可用成功解决此问题,通过判断当前已存储的字符串中,是否已经有新的字符串了。如果存在则将其删除至oldchar位置,来保证当前字符串中没有重复字符串。

【代码实现】

private int length = 0;
private StringBuilder sb = new StringBuilder();
public int lengthOfLongestSubstring(String s)
{
int count = 0;
for(char c : s.toCharArray())
{
if(sb.toString().contains(String.valueOf(c)))
{
//查找当前已经存在相同字符的位置,并将其之前的删除,仅保留后续字符,避免字符重复。
int frist_index = sb.toString().indexOf(c) + 1;
String str = sb.toString().substring(frist_index);
sb = new StringBuilder(str);
sb.append(c);
//由于已经更新了字符串信息,所以长度信息同步更新。
count = count - frist_index + 1;
continue;
}
sb.append(c);
count ++;
//保证当前length中存储的是最长字符串长度
length = Math.max(length, count);
} return Math.max(length, count);
}

【后续】

查看了leetcode上的相关解决思路,有个方法比较好,通过一个set来存储,避免了重复创建对象,这个方法还是比较好的,在此也贴下代码,以便后续学习用。

    public int lengthOfLongestSubstring2(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
// try to extend the range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}

LeetCode之Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  6. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

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

  8. LeetCode[3] Longest Substring Without Repeating Characters

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

  9. 【leetcode】Longest Substring Without Repeating Characters

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

随机推荐

  1. Ubuntu16.04系统Python3相关环境或模块安装

    前提:一般用户安装都命令前都需要sudo ,或者在root用户下 1.Ubuntu 16.04 安装PyCharm Ubuntu 16.04 安装PyCharm 本文通过第三方源安装PyCharm,好 ...

  2. php 安装Memcache扩展

    转载地址:http://www.tuicool.com/articles/EB3imm 文章概述:由于当前机器安装的php,是用yum安装,现在需要使用到memadmin做一些监控, memadmin ...

  3. @Override笔记

    作用:用来保证正确重写方法,当你重写方法出错时,比如方法名误写,或者漏掉参数,编译器会提示编译错误. 使用场景:继承父类,重写父类方法:实现接口,实现接口方法. 备注:jdk1.5之允许在继承时使用, ...

  4. Mac下需要安装的一些软件及常用的配置文件

    常用软件配置文件 1..gitconfig # This is Git's per-user configuration file. [user] name = 张文 email = zhangwen ...

  5. 动态 K th

    每一棵线段树是维护每一个序列前缀的值在任意区间的个数,如果还是按照静态的来做的话,那么每一次修改都要遍历O(n)棵树,时间就是O(2*M*nlogn)->TLE考虑到前缀和,我们通过树状数组来优 ...

  6. [翻译]CSS模块-未来的编码方式

    前言 这是Glen Maddern发布于2015年8月19日的一篇文章,主要是之前翻译的文章<理解CSS模块方法>里提到这篇文章,现在算是顺藤摸瓜跟进来看看. 这里的翻译都是根据我自己的理 ...

  7. Springboot--配置文件注解

    使用注解1 1.resouse中新建application.proprities jdbc.username=root jdbc.password=123 jdbc.driverClassName=f ...

  8. java中把指数形式的数字转为正常形式显示

    /** * 当浮点型数据位数超过10位之后,数据变成科学计数法显示.用此方法可以使其正常显示. * @param value * @return Sting */ public static Stri ...

  9. Apache, service httpd stop, Address already in use:

    service httpd stopStopping httpd:                                            [FAILED][root@testtest ...

  10. 转载:Service Mesh:重塑微服务市场--敖小剑

    转载地址:https://skyao.io/talk/201805-service-mesh-rebuild-microservice-market/ 重点: 不要太过关注  Service Mesh ...