求最长不重复字符串-源码

 

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.

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        //方法一
//         //用HashMap来存储一个字符,字符作为键,下标作为值,在相同字符时,就可以实现覆盖
//         HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
//         //用于标记每一次重新计算字符的起点
//         int j = 0;
//         int max = 0;//最大长度
//         for(int i = 0;i<s.length();i++){
//             //注意char是不能作为键的!!!!
//             int c = s.charAt(i);
//             //判断是否在map中存在了,存在了则重新选择起点
//             if(map.containsKey(c)){
//                 //原来的位置的下一个即为起点
//                 //?为什么要取max不理解
//                 //懂了:abba是本来字第一次b和b重复时起点下标应该为2,当你往下再走到a时,map中保存着的是第一个a的下标,
//                 //但你的起点应该是他而应该要比他大的值,这是为了解决在选好起点后,起点字符左右会有相同的字符出现
//                 //所以不能单一的直接使用:map.get(c)+1,因为这样会获取到你选定的起点字符的前面的,这会出错
//                 j = Math.max(j,map.get(c)+1);
//             }
//             //将该字符和下标放入map
//             map.put(c,i);
//             //原来的max和现在第二个字符比较的最大长度
//             max = Math.max(max,i-j+1);
//         }
//         return max;
        
        //方法二,滑动窗口。
        int freq[] = new int[256];
        int l = 0;//区间为[l...r]
        int r = -1;
        int res = 0;//记录最大长度
        char[] str = s.toCharArray();
        //不断的移动窗口
        while(l < str.length){
            if(r+1 < str.length&&freq[str[r+1]]==0){
                r++;
                freq[str[r]]++;
            }else{
                freq[str[l]]--;
                l++;
            }
            
            res = Math.max(res,r-l+1);
        }
        
        return res;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要找到Java中的最长重复字符串,可以使用滑动窗口算法。滑动窗口是一个可变大小的窗口区域,用于遍历字符串并找到最长的不重复字符串。 以下是一个示例的Java代码实现,可以找到给定字符串最长重复字符串: ```java public static String findLongestSubstring(String value) { int max = 0, len = value.length(), temp = 0; HashMap<Character, Integer> map = new HashMap<>(); for(int i = 0; i < len; i++) { int left = map.getOrDefault(value.charAt(i), -1); if(temp >= i - left) { temp = i - left; } else { temp++; max = Math.max(temp, max); } map.put(value.charAt(i), i); } return max; } ``` 这个方法的时间复杂度是O(n),其中n是字符串的长度。空间复杂度是O(1),因为哈希表的大小不取决于输入字符串的长度。 请注意,这段代码中的方法`findLongestSubstring()`返回的是最长重复字符串的长度,而不是字符串本身。如果你需要返回字符串本身,你可以根据找到的最长长度来提取子字符串。 希望这个代码可以帮助到你!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [关于算法:查找字符串最长重复字符串所需的Java函数?](https://blog.csdn.net/weixin_42386504/article/details/114521295)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [JAVA练习98-最长不含重复字符的子字符串](https://blog.csdn.net/qq_48772498/article/details/122831289)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值