LeetCode:第三题
来源:互联网 发布:电脑连不上网络 编辑:程序博客网 时间:2024/06/02 03:49
LeetCode刷题第三题,参考牛客网大神的解法,利用Java实现第三题,题目如下:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequenceand not a substring.
解法如下:
class class Solution {
public int lengthOfLongestSubstring(String s) {
//首先判断是否为空
if(s == null || s.length()== 0) return 0;
//新建一个map来存储char
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int leftBound = 0;
int max = 0;
for(int i=0; i < s.length(); ++i) {
char c = s.charAt(i);
//窗口左边可能为下一个字符,或者不变
leftBound = Math.max(leftBound, (map.containsKey(c))? map.get(c)+1 : 0);
max = Math.max(max, i-leftBound+1);//当前窗口长度
map.put(c,i);
}
return max;
}
}
- 第三周leetcode题
- leetcode第三题
- Leetcode第三题python
- LeetCode:第三题
- LeetCode算法第三题
- leetcode第三题-----Palindrome Number
- 关于leetcode第三题的巧妙解法
- leetcode 第三题题目及解题思路
- LeetCode Weekly Contest 56 第三题
- leetcode-python 第三周
- leetcode-第三周
- [leetcode] 第三周作业
- 第三周LeetCode
- LeetCode题解 第三周
- leetcode 第三题 Longest Substring Without Repeating Characters
- LeetCode 第三题,Longest Substring Without Repeating Characters
- leetcode第三题Longest Substring Without Repeating Characters java
- Leetcode第三题_Longest Substring Without Repeating Characters
- HDU 6208 The Dominator of Strings
- Python3:《机器学习实战》之Logistic回归(2)最佳回归系数确定
- 面向普通开发者的机器学习入门
- 事件三要素(实例)
- Java数据类型
- LeetCode:第三题
- Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
- C语言算法,八皇后问题,回溯算法
- 懒人实现Android城市列表
- 关于OkHttp解析数据添加到RecyclerView
- DS之单链表
- 1005. 继续(3n+1)猜想 (25)
- 简单的购物车...
- mysql注册驱动的三种方式