LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
1 时间复杂度O(N²)的算法
//
// Solution.h
// LeetCodeOJ_003_LongestUniqueSubstr
//
// Created by feliciafay on 11/21/13.
// Copyright (c) 2013 feliciafay. All rights reserved.
//
#ifndef LeetCodeOJ_003_LongestUniqueSubstr_Solution_h
#define LeetCodeOJ_003_LongestUniqueSubstr_Solution_h
#include <iostream>
#include <string>
#include <map>
using std::string;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
unsigned long limit =s.length();
if(limit==0)
return 0;
int start = 0;
int p = 0;
int q = 0; //q对程序其实没作用,主要用来debug的时候观察。q与p的区别是,p表示当前进行探测的字符,q表示p的前面一位字符。
unsigned long max_length = 1;
unsigned long cur_length = 1;
string sub1;
std::map<char,int> char_int_map;
std::map<char,int>::iterator it = char_int_map.begin();
while(p<limit) {
it = char_int_map.find(s[p]);
if(it == char_int_map.end()) {
q = p;
// std::cout<<"1-1 q: "<<q<<std::endl;
char_int_map.insert(std::p