数据结构与算法刷题每日记录:无重复字符的最长字串

无重复字符的最长字符串

 这题个人思路:使用滑动窗口算法,滑动窗口一般用来解决一些查找满足一定条件的连续区间的性质(长度等)的问题,左指针右指针一开始指向字符串开头,通过循环,右指针向字符串右边移动,并使用Map记录每个字符所在的索引下标,当发现map内存在某字符索引下标(说明出现重复字符串),将左指针进行右移,右移动位置为map内存在重复字符位置的下一字符(即该重复字符的下一字符)索引与当前左指针所在位置索引的较大一方(因为abba,第一次b重复,左指针指向索引为2的位置,但是又遇到a重复,“map内存在字符位置的下一字符”会将左指针指向索引1的位置,这样导致左指针向右移动了,是不对的,所以要跟当前左指针进行比较,取较大的一方),至于长度在右指针进行一次移动就进行一次最大长度记录,记录每次右指针减去左指针(索引从0开始,长度需要加1)的长度与上一次记录的比较取最大值,更新最大长度记录即可

class Solution {
    //使用滑动窗口
    public int lengthOfLongestSubstring(String s) {
        //左指针
        int start=0;
        //记录长度
        int length=0;
        Map<Character,Integer> map=new HashMap<Character,Integer>();
        for(int i=0;i<s.length();i++){
           if(map.get(s.charAt(i))==null){
            //更新当前字符串索引
            map.put(s.charAt(i),i);
           }else{
            //将左节点指向上一个相同的节点下一节点
            start=Math.max(start,map.get(s.charAt(i))+1);
            //更新索引
            map.put(s.charAt(i),i);
           }
           length=Math.max(length,i-start+1);
        }
        return length;
    }
}

LeetCode耗时较短解法

class Solution {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int max = 0;
        boolean[] has = new boolean[128];
        for (int l = 0,r = 0;r < chars.length;r++){
            while (has[chars[r]]){
                has[chars[l]] =false;
                l++;
            }
            has[chars[r]] = true;
            max = Math.max(max,r-l+1);
        }
        return max;
    }
}

通过boolean值来判断是否重复

在latex中,插入跨双栏的图片是很简单的。你只需要在插入图片的代码前加上`\begin{figure*}[!t`和在代码后加上`\end{figure*}`。这样,图片就会跨越两栏并居中显示。例如,你可以使用以下代码插入跨双栏的图片: ``` \begin{figure*}[!t] \centering \includegraphics<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Latex写文章时插入单栏图片和双栏图片方法](https://blog.youkuaiyun.com/Time_Memory_cici/article/details/129381838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [latex表格(图片)跨双栏](https://blog.youkuaiyun.com/qq_39463175/article/details/106589433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [LaTeX--在双栏格式中插入一张图片](https://blog.youkuaiyun.com/qq_37933128/article/details/127214421)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值