• LeetCode 3: 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.

    思路:

    题目要求一个字符串中最长的没有重复字符的字符串长度,很明显可以用哈希表来实现,对出现的字符,哈希表中置1,两个指针一个指向头一个指向尾。并记录最长的没有重复字符的最长字符串的长度。

    代码:

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
    		int start = 0;
    		int tail = 0;
    		map<char,int> map_str;
    		int max = 0;
    		int count = 0;
    		while(start<s.size()&&tail<s.size()){
    			if(map_str[s[tail]]==0){
    				map_str[s[tail]]++;
    				count ++;
    				tail++;
    				if(max<count){
    					max = count;
    				}
    			}else{
    				map_str[s[start]]--;
    				start++;
    				count--;
    			}
    		}
    		return max;
    	}
    };
    
  • 相关阅读:
    用SQL语言操作数据
    用表组织数据
    第一个C#程序
    利用CSS3制作网页动画
    CSS3美化网页元素
    列表、表格与媒体元素
    表单
    HTML5基础
    使用Java编译思想
    Day06:方法 / 猜字母游戏
  • 原文地址:https://www.cnblogs.com/xiamaogeng/p/4358533.html
Copyright © 2020-2023  润新知