【题目描述】

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. ElasticSearch(六) Elasticsearch在Thinkphp5.0中的使用

    首先下载需要引入的类库 链接:https://pan.baidu.com/s/1XEXviLoWM-ypwJ_B0jXqlg 密码:u54t //Elasticsearch.zip类库压缩包地址 然后 ...

  2. elasticsearch搜索集群基础架构

    1.  elasticsearch cluster搭建 http://www.cnblogs.com/kisf/p/7326980.html   为了配套spring boot,elasticsear ...

  3. 关于git pull时出现的问题及解决反思

    前因 在前面由于已经git过一次,按照娄老师的要求,代码一个一个commit,所以删掉之前的git仓库(用la查看,使用rm -rf .git删除).但远程origin已经存在,所以执行git rem ...

  4. 20145303《Java程序设计》实验三实验报告

    20145303<Java程序设计>实验三实验报告 ssh公钥配置及git安装: eclipse中git配置: 队友链接: http://www.cnblogs.com/5321z/p/5 ...

  5. hello java !

    我对于计算机性编程性质的课程一直没有很好的悟性,但功夫不服有心,最近自己学习视频课程,随时关注娄老师的博客,慢慢的对于java编程有了新的认识,也用eclipse软件进行了简单java的编译. 了解的 ...

  6. 【前端】jQuery选择器$()的实现原理

    今天三七互娱技术面试的时候面试官问了我这个问题,当时一脸懵逼,于是好好总结一下. 当我们使用jquery选择器的时候,$(s).回默认去执行jquery内部封装好的一个init的构造函数每次申明一个j ...

  7. Linux升级内核总结

    Linux内核升级总结. 一.编译内核步骤 1.#uname –r 确定系统的原内核版本,然后下载较新版本的Linux内核源码包 http://www.kernel.org/pub/linux/ker ...

  8. LeetCode——Integer Break

    Question Given a positive integer n, break it into the sum of at least two positive integers and max ...

  9. php 模拟 asp.net webFrom 按钮提交事件

    由于公司需要php方面的项目开发,php刚刚入门,在写按钮提交过程中,asp.net里的按钮事件更好些.先看下面的代码, <? require_once '../inc/EventHelper. ...

  10. Struts2框架学习第二章——Struts2下的HelloWorld

    本章要点 —  Struts 2的下载和安装 — 纯手工创建一个Web应用 — 纯手工创建一个Struts 2应用 — 实现Struts 2的Action — 配置Struts 2的Action — ...