【题目描述】

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. selenium 难定位元素,时间插件,下拉框定位,string

    1.元素定位 ID定位元素: findElement(By.id(“”)); 通过元素的名称定位元素: findElement(By.name(“”)); 通过元素的html中的位置定位元素: fin ...

  2. python 内建函数isinstance的用法以及与type的区别

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  3. 在VS2012中采用C++中调用DLL中的函数(4)

    转自:http://www.cnblogs.com/woshitianma/p/3683495.html 这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天 ...

  4. 简单的linux压力测试工具webbench

    wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...

  5. 20145312 《Java程序设计》第10周学习总结

    20145312 <Java程序设计>第10周学习总结 学习总结 一. 什么是网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的 ...

  6. HAL编译问题

    1 make:进入目录'/opt/FriendlyARM/tiny4412/android/android-4.1.2'make: *** 没有规则可以创建“out/target/product/ge ...

  7. 轻谈Normalize.css

    Normalize.css 是 * ? Normalize.css只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相比于传统的CSS reset , Normali ...

  8. JS时间和字符串的相互转换 Date+String

    1.js字符串转换成时间 1.1方法一:输入的时间格式为yyyy-MM-dd function convertDateFromString(dateString) { if (dateString) ...

  9. iOS开发值得学习的Demo

    一.HXWeiboPhotoPicker - 仿微博照片选择器 GitHub地址:https://github.com/LoveZYForever/HXWeiboPhotoPicker 二.AFNet ...

  10. Commons Configuration之一简介

    转载自(https://my.oschina.net/u/2000201/blog/486327) 1    简介 Commons Configuration软件类库提供通用配置接口,使Java应用程 ...