Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140921
  • 博文数量: 56
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-08 14:43
个人简介

慢慢来

文章分类

全部博文(56)

文章存档

2017年(5)

2016年(2)

2015年(6)

2014年(28)

2013年(5)

2012年(10)

我的朋友

分类: C/C++

2014-08-20 21:08:27

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 {
  2. public:
  3.     int lengthOfLongestSubstring(string s) {
  4.           int i=0, j=0, max_i=0, max_j=0, max_length=0;
  5.           map<char,int> map_store;
  6.           map<char,int>::iterator map_store_it;
  7.       
  8.           while(j < s.length()){
  9.             map_store_it = map_store.find(s[j]);
  10.             if(map_store_it == map_store.end()){
  11.                 map_store.insert( pair <char,int> (s[j], j));
  12.                 if( (j-i+1) > max_length ){
  13.                     max_i = i;
  14.                     max_j = j;
  15.                     max_length = (j-i+1);
  16.                 }
  17.             } else{
  18.                 int k = map_store_it->second;
  19.                 map_store.erase(map_store_it);
  20.                 while(i < k){
  21.                     map_store_it = map_store.find(s[i]);
  22.                     map_store.erase(map_store_it);
  23.                     i++;
  24.                 }
  25.                 map_store.insert( pair <char,int> (s[j], j));
  26.                 i++;
  27.             }
  28.             j++;
  29.           }
  30.           
  31.           return max_length;
  32.       }
  33. };

阅读(528) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~