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. Database--事务

    参考 https://segmentfault.com/a/1190000012669504?utm_source=tag-newest https://blog.csdn.net/qq_371559 ...

  2. EUI库 - 容器

      eui.UILayer UILayer是Group的子类它只有一个功能,到放到场景上后,宽高永远和场景宽度一致   Group Group 是自动布局的容器基类.如果包含的子项内容太大需要滚动显示 ...

  3. 22 ~ express ~ 内容评论实现

    1,使用 ajax 提交评论内容 给 api.js 2,数据库 contents 增加评论字段 3,后台路由 api.js 接收并完成存储 /** 增加评论(用户,内容,时间) */ router.p ...

  4. C++基础--随机数

    C/C++要产生随机数的方法一般是采用rand()函数核srand()函数来实现的. rand()函数返回的是一个伪随机数,这个函数内部采用线性同余法来实现伪随机数,而这个伪随机数是在一定范围内可以看 ...

  5. Java8大排序算法

    一.冒泡排序 基本思想:通过对待排序序列此前向后,依次比较相邻元素的值,若发现逆序则进行交换,使得较大的值从前面移动到后面,       类似于水下的气泡一样(是所有排序算法中效率最低的) publi ...

  6. 剑指offer_2.1_Day_5

    输入一个链表,按链表从尾到头的顺序返回一个ArrayList. import java.util.ArrayList; public class Solution { public ArrayList ...

  7. jQuery下拉框联动(JQ遍历&JQ中DOM操作)

    1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...

  8. Jquery获取html标签,包含该标签本身

    $(".test").prop("outerHTML"); 原生JS DOM里有一个内置属性 outerHTML,用来获取当前节点的html代码(包含当前节点) ...

  9. Vue.js(20)之 封装字母表(是这个名字吗0.0)

    HTML结构: <template> <div class="alphabet-container"> <h1>alphabet 组件</ ...

  10. 系统学习python第七天学习笔记

    1.get方法用法补充 info = {'name':'王刚蛋','hobby':'铁锤'} while True: data = input('请输入:') val = info.get(data, ...