LeetCode:3.无重复字符的最长字串(longest-substring-without-repeating-characters) Java完整代码

LeetCode:3.无重复字符的最长字串(longest-substring-without-repeating-characters)
题解和思路

完整代码

import java.util.*;
/**
 * 在这里给出对类 LC3LStrWithoutReChar 的描述。
 * 
 * @作者(yequan17)
 * @版本(2021.12.7)
 */
public class LC3LStrWithoutReChar
{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        System.out.println(lengthOfLongestSubstring(s));
        sc.close();
    }
    public static int lengthOfLongestSubstring(String s) {
        int length=0;
        Map<Character,Integer> map=new HashMap<>();
        for(int end=0,start=0;end<s.length();end++){
            char c=s.charAt(end);
            if(map.containsKey(c)){
                start=Math.max(map.get(c)+1,start);
            }
            length=Math.max(end-start+1,length);
            map.put(s.charAt(end),end);
        }
        return length;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值