zoukankan      html  css  js  c++  java
  • Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

    /*
    题目意思:就最长的不出现重复字母的字符串的长度
    一个开始位置的变量记录开始位置,
    若当前的字符出现重复时,直接将开始位置的变量跳转到当前位置,并计算长度
    */
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) 
        {
            int Count[256];
            int i;
            int len ;
            int maxLen = 0;
            int idx = -1;
    
            memset( Count, -1, sizeof(Count) );
            //cout << Count[0] << " " << Count[1] <<endl;
            len = s.size();
    
            for( i = 0; i < len; i++ )
            {
               if( Count[s[i]] > idx )
               {
                   idx =  Count[s[i]];
               }
    
               if( i - idx > maxLen )
               {
                    maxLen = i - idx;
               }
               Count[s[i]] = i;
            }
    
            return maxLen;
        }
    };
    当你的才华还撑不起你的野心时,那你就应该静下心来学习。
  • 相关阅读:
    javajava.lang.reflect.Array
    基于annotation的spring注入
    jquery插件
    spring的注入方式
    jqueryajax
    javascript基础
    xml基础
    js 获取FCKeditor 值
    TSQL 解析xml
    Linq
  • 原文地址:https://www.cnblogs.com/aceg/p/4426224.html
Copyright © 2011-2022 走看看