Given a string, find the longest substring without any repeating characters and return the length of it. The input string is guaranteed to be not null.

For example, the longest substring without repeating letters for "bcdfbd" is "bcdf", we should return 4 in this case.

public class Solution {
public int longest(String input) {
// Write your solution here
if (input.length() == 0) {
return 0;
}
char[] charArr = input.toCharArray();
Set<Character> set = new HashSet<>();
int res = 0;
int start = 0, i = 0;
while (i < charArr.length) {
char cur = charArr[i];
if (!set.contains(cur)) {
set.add(cur);
res = Math.max(res, i - start + 1);
i += 1;
} else {
set.remove(charArr[start]);
start += 1;
}
}
return res;
}
}

[Algo] 253. Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

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

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

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

  3. Longest Substring Without Repeating Characters

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

  4. 3. Longest Substring Without Repeating Characters(c++) 15ms

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

  5. 【leetcode】Longest Substring Without Repeating Characters

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

  6. Longest Substring Without Repeating Characters(C语言实现)

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

  7. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  8. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  9. Longest Substring Without Repeating Characters (c#)

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

随机推荐

  1. POJ1833 & POJ3187 & POJ3785

    要是没有next_permutation这个函数,这些题觉得还不算特别水,不过也不一定,那样可能就会有相应的模板了.反正正是因为next_permutation这个函数,这些题包括之前的POJ1226 ...

  2. Beyond Compare 文件对比工具的使用

    Beyond Compare 文件对比工具的使用 Beyond Compare 工具下载地址: http://www.onlinedown.net/soft/633850.htm 本文下载地址:E:\ ...

  3. MongoDB四-操作索引

    转自: http://www.cnblogs.com/huangxincheng/archive/2012/02/29/2372699.html 我们首先插入10w数据,上图说话: 一:性能分析函数( ...

  4. 深入理解Canvas Scaler

    Canvas Scaler: 这是一个理解起来相当繁琐复杂的一个组件,但又是一个至关重要的组件,不彻底了解它,可以说对UGUI的布局和所谓的“自适应”就没有一个完整的认识. Canvas Scale指 ...

  5. part5 城市页面列表开发

    1.配置路由 先在router文件夹中,创建一个路由.引入组件 { path: '/city', name: 'HelloCity', component: city, meta: { name: ' ...

  6. Java web项目所需技术*(大概)

    实施java 的web项目需要掌握的技术如下: 1.java语言 2.  面向对象分析设计 XML 语言 网页脚本语言 数据库 应用服务器 集成开发环境 .java语言:JSP.Servlet.JDB ...

  7. 自定义View不显示的问题

    问题描述: 我自定义了一个把 SwipeRefreshLayout 和 RecyclerView 封装在一起的 View ,但是发现 List 不能正常的显示出来,本以为是数据源出现问题,debug了 ...

  8. python类(2)

    #从python开始学习编程 学习笔记 以后看书时注意一下书上表述:好像是类属性attribute,对象特性property,对象方法 1.对于一个类下的全部个体来说,某些属性可能存在个体差异.不是所 ...

  9. php优惠券生成-去重

    记录一次优惠券生成-去重 方法一 /** * 生成批量礼品消费券 */ public function giftCardAddOp() { //接收get值 $num = $_GET['gift_nu ...

  10. mysql比较运算,逻辑运算,范围查询,模糊查询

    比较运算 > < =  !=  <>   <=  >=  逻辑运算  and  or  not 范围查询  in   模糊查询  like             ...