-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_LongestSubstringWithoutDup.cpp
More file actions
208 lines (175 loc) · 4.48 KB
/
Copy path48_LongestSubstringWithoutDup.cpp
File metadata and controls
208 lines (175 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题48:最长不含重复字符的子字符串
// 题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子
// 字符串的长度。假设字符串中只包含从'a'到'z'的字符。
#include <string>
#include <iostream>
// 方法一:蛮力法
bool hasDuplication(const std::string& str, int position[]);
int longestSubstringWithoutDuplication_1(const std::string& str)
{
int longest = 0;
int* position = new int[26];
for(int start = 0; start < str.length(); ++start)
{
for(int end = start; end < str.length(); ++end)
{
int count = end - start + 1;
const std::string& substring = str.substr(start, count);
if(!hasDuplication(substring, position))
{
if(count > longest)
longest = count;
}
else
break;
}
}
delete[] position;
return longest;
}
bool hasDuplication(const std::string& str, int position[])
{
for(int i = 0; i < 26; ++i)
position[i] = -1;
for(int i = 0; i < str.length(); ++i)
{
int indexInPosition = str[i] - 'a';
if(position[indexInPosition] >= 0)
return true;
position[indexInPosition] = indexInPosition;
}
return false;
}
// 方法一:动态规划
int longestSubstringWithoutDuplication_2(const std::string& str)
{
int curLength = 0;
int maxLength = 0;
int* position = new int[26];
for(int i = 0; i < 26; ++i)
position[i] = -1;
for(int i = 0; i < str.length(); ++i)
{
int prevIndex = position[str[i] - 'a'];
if(prevIndex < 0 || i - prevIndex > curLength)
++curLength;
else
{
if(curLength > maxLength)
maxLength = curLength;
curLength = i - prevIndex;
}
position[str[i] - 'a'] = i;
}
if(curLength > maxLength)
maxLength = curLength;
delete[] position;
return maxLength;
}
// ====================测试代码====================
void testSolution1(const std::string& input, int expected)
{
int output = longestSubstringWithoutDuplication_1(input);
if(output == expected)
std::cout << "Solution 1 passed, with input: " << input << std::endl;
else
std::cout << "Solution 1 FAILED, with input: " << input << std::endl;
}
void testSolution2(const std::string& input, int expected)
{
int output = longestSubstringWithoutDuplication_2(input);
if(output == expected)
std::cout << "Solution 2 passed, with input: " << input << std::endl;
else
std::cout << "Solution 2 FAILED, with input: " << input << std::endl;
}
void test(const std::string& input, int expected)
{
testSolution1(input, expected);
testSolution2(input, expected);
}
void test1()
{
const std::string input = "abcacfrar";
int expected = 4;
test(input, expected);
}
void test2()
{
const std::string input = "acfrarabc";
int expected = 4;
test(input, expected);
}
void test3()
{
const std::string input = "arabcacfr";
int expected = 4;
test(input, expected);
}
void test4()
{
const std::string input = "aaaa";
int expected = 1;
test(input, expected);
}
void test5()
{
const std::string input = "abcdefg";
int expected = 7;
test(input, expected);
}
void test6()
{
const std::string input = "aaabbbccc";
int expected = 2;
test(input, expected);
}
void test7()
{
const std::string input = "abcdcba";
int expected = 4;
test(input, expected);
}
void test8()
{
const std::string input = "abcdaef";
int expected = 6;
test(input, expected);
}
void test9()
{
const std::string input = "a";
int expected = 1;
test(input, expected);
}
void test10()
{
const std::string input = "";
int expected = 0;
test(input, expected);
}
int main(int argc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
return 0;
}