3. Longest Substring Without Repeating Characters【leetcode】java,算法,Substring实现,子串,HashMap

3. Longest Substring Without Repeating Characters

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 subsequence and not a substring.

问题:

给定一个字符串,找到最长的子串的长度没有重复字符。
实例:
“abcabcbb”,答案是“ABC”,它的长度是3。
“bbbbb”,答案是“B”,长度为1。
“pwwkew”,答案是“wke”,以3的长度。注意,答案必须是一个字符串,“pwke”是子串。

实现方式:

1.获取字符串长度
2.纪录当前值为左标记
3.判断当前字母是否出现过,若出现过就更新当前位置为新的左标记
4.如果当前 值比res大则更新res长度

方法:使用HashMap这样,遇到重复就跳过循环,节约时间和空间

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         Map<Character,Integer> map =new HashMap<>();
 4         int res =0,len=s.length();
 5         for(int i=0,j=0;j<len;j++){
 6             if(map.containsKey(s.charAt(j))){
 7                 i=Math.max(map.get(s.charAt(j)),i);
 8             }
 9             map.put(s.charAt(j),j+1);
10             res =Math.max(j-i+1,res) ;
11         }
12         return res;
13     }
14 }
时间: 2024-11-06 07:27:19

3. Longest Substring Without Repeating Characters【leetcode】java,算法,Substring实现,子串,HashMap的相关文章

Longest Substring Without Repeating Characters leetcode java

题目: 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 s

LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

题目链接 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

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q

Longest Substring Without Repeating Characters Leetcode

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.

LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

problem: 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 long

leetcode03-Longest Substring Without Repeating Characters之Java版本

我的leetcode之旅,该篇章主要完成使用Java实现算法.这是第三篇Longest Substring Without Repeating Characters 全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github:多谢: 1.题目简介: Given a string, find the length of the longest substring without repeating char

leetcode longest substring without repeating characters(medium) /java

上题: 最简单粗暴的方法: 1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 String s1=new String(); 4 char[] c=s.toCharArray(); 5 int len=s.length(); 6 boolean[] f=new boolean[1000]; 7 int i=0; 8 for(i=0;i<1000;i++) 9 f[i]=false; 10 i

3. Longest Substring Without Repeating Characters寻找不重复的最大子串

首先弄清楚Substring和Subsequence,前者是子串,要求连续,后者是子序列,可以不连续 public int lengthOfLongestSubstring(String s) { /* 一开始自己想的方法:两个指针,一个作为开始,一个用来遍历,复杂度O(N) */ int l = s.length(); if (l==0) return 0; char[] str = s.toCharArray(); int res = 1; Set<Character> set = new

3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)

题目意思:求字符串中,最长不重复连续子串 思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值 eg:a,b,c,d,e,c,b,a,e 0 1 2 3 4 0 1 5 3 4 0 6 5 3 4 7 6 5 3 4 7 6 5 3 8 再次出现c时,将start值置为map[c]+1=3,对于下一个b,因为map[b]<start,则直接map[b]=i 1 class Solution { 2 public: 3 int len

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana