zoukankan      html  css  js  c++  java
  • 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目:

      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.

    思路:

      初始字符上一次出现的位置-1,初始子串开始位置-1。对字符串中每个字符元素遍历,判断更新子串的开始位置及最大长度,循环结束后 返回最大长度max. 

    public class Solution {
        public int lengthOfLongestSubstring(String s) {
           int[] local=new int[128];
           int index=-1;
           int max=0;
           
           for(int a=0;a<local.length;a++){
               local[a]=-1;
           }
           
           for(int i=0;i<s.length();i++){
               
              if(local[(int)s.charAt(i)]>index){
                  index=local[(int)s.charAt(i)];
              }
              
              if(i-index>max){
                  max=i-index;
              }
              
              local[(int)s.charAt(i)]=i;
           }
           
           return max;
        }
    }
    

      

  • 相关阅读:
    自定义轮播图、自定义集合控件的实现
    推荐一个比较好用的工具
    博客迁移至简书
    ReactiveCocoa学习资料
    Xcode常用快捷键
    iOS自定义字体
    cocoapod的安装与使用
    iOS开发笔记
    UIWebView的三种加载方式
    项目常用第三方库
  • 原文地址:https://www.cnblogs.com/zhstudy/p/5983853.html
Copyright © 2011-2022 走看看