LeetCode(3) Longest Substring Without Repeating Characters

蔚落 2022-07-17 15:26 247阅读 0赞

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

  1. public class Solution {
  2. public int lengthOfLongestSubstring(String s) {
  3. int length = s.length();
  4. int i=0,j=1;
  5. int maxlen=0;
  6. while(j<length){
  7. if(s.charAt(i)!=s.charAt(j)){
  8. j++;
  9. }else{
  10. maxlen = max(maxlen,j-i);
  11. i++;
  12. j++;
  13. }
  14. }
  15. return maxlen;
  16. }
  17. private int max(int maxlen,int i){
  18. int temp;
  19. if(maxlen>=i)
  20. temp = maxlen;
  21. else
  22. temp = i;
  23. return temp;
  24. }
  25. }

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读