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;
        }
    }
    

      

  • 相关阅读:
    GlusterFS-分布式存储集群部署
    keepalived+HAproxy集群部署
    LB-HAproxy负载均衡部署
    Pacemaker高可用环境实践
    Nginx-负载均衡部署
    LB-LVS常见模式NAT/DR部署
    HTTPS原理、应用
    LDAP-autofs挂载用户验证
    GPG-非对称加密
    大数据入门学习(Linux)
  • 原文地址:https://www.cnblogs.com/zhstudy/p/5983853.html
Copyright © 2011-2022 走看看