【题目描述】

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. 用74HC165读8个按键状态

    源:用74HC165读8个按键状态 源:74LV165与74HC595 使用 74LV165说明: 74LV165是8位并行负载或串行输入移位寄存器,末级提供互补串行输出(Q7和Q7).并行负载(PL ...

  2. 根据iframe获取window

    今天使用layui弹出窗口,需要将函数写在弹出的窗口,但是按钮事件是在父层窗口绑定的,这样就要在父层窗口调用子层窗口的函数. 子层函数与父层函数 function topup() { console. ...

  3. 20145303 《Java程序设计》第六周学习总结

    20145303 <Java程序设计>第六周学习总结 教材学习内容总结 第十章:输入/输出 (InputStream与OutputStream) 1.Java将输入/输出抽象化为串流,数据 ...

  4. 20145314郑凯杰《信息安全系统设计基础》第7周学习总结 part B

    20145314郑凯杰<信息安全系统设计基础>第7周学习总结 part B 上篇博客反思与深入 首先根据本周第一篇博客,娄老师给我的评论,我开始进行局部性的深入研究: 分为两个步骤,一是知 ...

  5. C++实现最基本的LRUCache服务器缓存

    目录: 一.介绍: 二.数据结构: 三.主要的两个函数接口Put()和Get(): 四.C++代码实现: 后台开发必备知识,不过我不是搞这个的,只是因为很久以前就想写这些东西,事情多,拖到现在.写的过 ...

  6. USB详解

    USB作为一种串行接口,应用日益广泛.如同每个工程设计人员必须掌握I2C,RS232这些接口一样,我们也必须掌握USB.但是USB的接口协议实在有点费解,Linux UCHI驱动作者之一Alan St ...

  7. DCU项目总结

    1.什么是DCU 在某些基站无法覆盖的地方,如大型体育馆内部1楼.2楼..,此时通过DCU为这些地方提供信号 2.DCU组成 3.我们需要做的 PC通过进入UMPT网关,在一个网页中使用自定义指令集控 ...

  8. 【bzoj4423】[AMPPZ2013]Bytehattan(平面图转对偶图+并查集)

    题目传送门:bzoj4423 如果是普通的删边判连通性,我们可以很显然的想到把操作离线下来,倒着加边.然而,这题强 制 在 线. 虽然如此,但是题目所给的图是个平面图.那么我们把它转成对偶图试试看? ...

  9. [Network Architecture]ResNext论文笔记(转)

    文章地址: https://blog.csdn.net/u014380165/article/details/71667916 论文:Aggregated Residual Transformatio ...

  10. win7打开ftp步骤

    FTP是很方便文件传输的功能 打开ftp xftp连接 传输测试 如果连接不通的话,有可能是防火墙的问题