import java.util.HashSet;
import java.util.Set; /**
* Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
*
* Created by lverpeng on 2017/6/26.
*
* 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 LongestSubstring { /**
* 两个指针:一个指向当前字符,一个指向当前子串开始的位置
* 将指向当前子串的指针依次向后移动,判断当前哈希表(用字符作为key,value不重要,可以直接用set)中是否存在当前字符
* 如果已经存在,说明找到一个子串
* 将指向当前子串的指针向后移动一个位置,作为新的子串起始位置
* 计算刚刚结束子串的长度,和之前长度作比较,选出较大的一个
* 如果不存在,更新当前子串的长度,加1
* 将当前字符加入hash表,将当前指针向后移动,重复上面的操作
*
*
*
* @param str
* @return
*/
public int searchLongestSubstring (String str) {
int lastIndext = 0;
int maxLen = 0;
Set<Character> charSet = new HashSet<Character>();
for (int i = 0; i < str.length(); i++) {
if (charSet.contains(str.charAt(i))) {
int newLen = i - lastIndext;
maxLen = Math.max(newLen, maxLen);
lastIndext ++;
} else {
maxLen ++;
}
charSet.add(str.charAt(i));
}
return maxLen;
} public static void main(String[] args) {
LongestSubstring longestSubstring = new LongestSubstring();
System.out.println(longestSubstring.searchLongestSubstring("abcabcbb"));
}
}

leetcode — median-of-two-sorted-arrays的更多相关文章

  1. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  2. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

  4. Leetcode Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  5. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  6. Leetcode: Median of Two Sorted Arrays. java.

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  7. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

  8. LeetCode——Median of Two Sorted Arrays

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  9. leetcode:Median of Two Sorted Arrays分析和实现

    这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...

  10. LeetCode Median of Two Sorted Arrays 找中位数(技巧)

    题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...

随机推荐

  1. 3R - 单词数

    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...

  2. (O)WEB:前端网站性能优化(原创)

    *从理论.实战编码.实战调试3个方面学习前端性能优化(包括页面加载时间和页面流畅度): -------------------------------理论----------------------- ...

  3. Asterisk 11 chan_sip.c: Failed to authenticate device 看不到IP的问题

    Asterisk 11 chan_sip.c: Failed to authenticate device 看不到IP的问题   没有验证过 原文地址 http://www.coochey.net/? ...

  4. [C#.net]将null值插入SQL Server的Datetime

    之前处理SQL Server可以为空时间字段总是设置时间的最小值和最大值,今天才发现也可以把null用C#的代码插入到sql内 使用可空的值类型,代码如下 public DateTime? Servi ...

  5. C#语言不常用语法笔记

    只看过3天C#语法书,了解个大概,与C++等不同之处,或者看开源遇到一些奇异用法,记录一下,脑子不够用的情况下,还是记笔记靠谱. ==================== 顺便吐槽下,这年头得会各种编 ...

  6. DedeCMS文章页去img图片width和height属性

    方法一:正则匹配去除 打开include/ arc.archives.class.php,查找代码: //设置全局环境变量 $this->Fields['typename'] = $this-& ...

  7. shell解析my.cnf配置文件

    my.cnf配置格式如下 vi my.cnf[client]port=3306socket=/tmp/mysql.socket [mysqld]port=3306server-id=1datadir= ...

  8. 《mysql必知必会》学习_第15章_20180806_欢

    第15章:联结表 P98 外键:外键为某个表的一列A,同时这一列包含另一个表的主键值B(B属于A,等于或者小于的关系) P99 select vend_name,prod_name,prod_pric ...

  9. Linux合上笔记本不进入休眠模式

      最近一个问题困扰了我很久,入职之前和人事说过工作中会用自己的电脑,但是人事还是坚持要给我发一个电脑,没办法,公司没有补贴,那就领了吧,索性将这个笔记本配置成了Fedora系统,用来当测试机,但是一 ...

  10. [转]kaldi中的在线识别----Online Recognizers

    转自: http://blog.csdn.net/wbgxx333/article/details/24932533 本文是kaldi学习联盟中@冒顿翻译的,下面是@冒顿的翻译结果,在这里感谢@冒顿的 ...