没有重复字符的最长子串

没有重复字符的最长子串

问题

暴力方法将涉及创建给定字符串的所有可能的子字符串,并找出哪个是最长的没有重复字符的子字符串。这将导致 tc:%ignore_a_1%(n^2)

Waymark Waymark

Waymark是一个视频制作工具,帮助企业快速轻松地制作高影响力的广告。

Waymark 79 查看详情 Waymark

最佳方法:
tc:o(n)
sc : o(256),用于使用大小为 256 的 int[]

class Solution {    public int lengthOfLongestSubstring(String s) {        int hash[] = new int[256];// size of all the ascii characters        Arrays.fill(hash,-1); // -1 to indicate these indexes don't have any character visited already        int left =0,right =0;        int max = 0;        while(right=left){// if the index of the character seen in the past is after the left pointer, then left pointer should be updated, to avoid duplicate in the window of substring                    left  = hash[c] + 1;// has to be 1 index after the index of last seen duplicate character                }            }            max = Integer.max(max, right-left+1);// keep track of mas window substring            hash[c]  = right;// update the character last seen index in hash            right++;        }        return max;    }}

以上就是没有重复字符的最长子串的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1008096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月1日 23:43:38
下一篇 2025年12月1日 23:43:59

相关推荐

发表回复

登录后才能评论
关注微信