Skip to content

Commit f9a04b7

Browse files
committed
剑指Offer--035-第一个只出现一次的字符位置--http://blog.csdn.net/gatieme/article/details/51319158
1 parent 1810628 commit f9a04b7

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <iostream>
22
#include <iomanip>
33

4+
45
using namespace std;
56

7+
68
#define __tmain main
79

810
#ifdef __tmain
@@ -19,56 +21,61 @@ using namespace std;
1921

2022
class Solution
2123
{
24+
2225
public:
26+
2327
int FirstNotRepeatingChar(string str)
2428
{
2529
if(str.length( ) == 0)
2630
{
2731
return -1;
2832
}
33+
2934
unsigned int length = str.length( );
3035
unsigned int i, j;
31-
bool flag = true;
36+
bool isrepeat = false;
37+
38+
3239
for(i = 0; i < length; i++)
3340
{
34-
flag = true;
41+
isrepeat = false;
3542
if(str[i] == '\0')
3643
{
3744
continue;
3845
}
3946
debug <<"str[" <<setfill('0')<<setw(2)<<i <<"] = " <<str[i] <<endl;
4047

48+
// 堆每一个当前字符i判断其后面有没有跟它相同的字符
4149
for(j = i + 1; j < length; j++)
4250
{
43-
debug <<"str[" <<i <<"] = " <<str[i] <<", ";
44-
debug <<"str[" <<j <<"] = " <<str[j] <<endl;;
51+
debug <<"str[" <<i <<"] = " <<str[i] <<", "
52+
<<"str[" <<j <<"] = " <<str[j] <<endl;;
4553

4654
if(str[j] == '\0')
4755
{
4856
continue;
4957
}
50-
else if(str[i] == str[j])
58+
else if(str[i] == str[j]) // 将所有与当前i位置字符相同的字符都置为'\0'
5159
{
5260
debug <<str[i] <<str[j] <<endl;
5361
str[j] = '\0';
54-
flag = false;
62+
isrepeat = true;
5563
//break;
5664
}
57-
5865
}
59-
if(flag == true)
66+
if(isrepeat == false)
6067
{
6168
return i;
6269
}
63-
}
64-
return -1;
70+
}
71+
return -1;
6572
}
6673
};
6774

6875
int __tmain( )
6976
{
7077
Solution solu;
7178
cout <<solu.FirstNotRepeatingChar("google") <<endl;
72-
79+
7380
return 0;
7481
}

035-第一个只出现一次的字符位置/bitmap.cpp renamed to 035-第一个只出现一次的字符位置/02-bitmap.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <iostream>
22
#include <cstring>
33

4+
45
using namespace std;
56

7+
68
#define __tmain main
79

810
#ifdef __tmain
@@ -18,7 +20,9 @@ using namespace std;
1820

1921
class Solution
2022
{
23+
2124
public:
25+
2226
int FirstNotRepeatingChar(string str)
2327
{
2428
int x[26] = {0}, y[26] = {0};
@@ -29,7 +33,7 @@ class Solution
2933
if('a' <= str[i] && str[i] <= 'z')
3034
{
3135
if(x[str[i] - 'a'] == 0)
32-
{
36+
{
3337
// 首次出现保存出现位置
3438
x[str[i] - 'a'] = i + 1;
3539
}
@@ -47,7 +51,7 @@ class Solution
4751
// 首次出现保存出现位置
4852
y[str[i] - 'A']= i + 1;
4953
}
50-
else
54+
else
5155
{
5256
// 出现多次, 就置标识-1
5357
y[str[i] - 'A'] = -1;

035-第一个只出现一次的字符位置/count.cpp renamed to 035-第一个只出现一次的字符位置/03-count.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include <iostream>
22
#include <cstring>
33

4+
45
using namespace std;
56

7+
68
#define __tmain main
79

810
#ifdef __tmain
@@ -19,8 +21,10 @@ using namespace std;
1921

2022
class Solution
2123
{
24+
2225
protected:
2326
int count[256];
27+
2428
public:
2529
int FirstNotRepeatingChar(string str)
2630
{
@@ -60,9 +64,9 @@ int __tmain( )
6064
int a = -1;
6165
unsigned int b = a;
6266
printf("%u\n", b);
63-
67+
6468
Solution solu;
6569
cout <<solu.FirstNotRepeatingChar("abcdefg") <<endl;
66-
70+
6771
return 0;
6872
}

035-第一个只出现一次的字符位置/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#链接
2-
-------
2+
-------
33
>牛客OJ:[第一个只出现一次的字符位置](http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking)
4-
>
4+
>
55
>九度OJ:http://ac.jobdu.com/problem.php?pid=1283
6-
>
6+
>
77
>GitHub代码: [035-第一个只出现一次的字符位置](https://github.com/gatieme/CodingInterviews/tree/master/035-第一个只出现一次的字符位置)
88
>
99
>CSDN题解:[剑指Offer--035-第一个只出现一次的字符位置](http://blog.csdn.net/gatieme/article/details/51319158)
1010
1111

12-
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
13-
| ------------- |:-------------:| -----:|
12+
| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
13+
| ------ |:------:| --------:|:----------:|
1414
|[035-第一个只出现一次的字符位置](http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [1283-第一个只出现一次的字符位置](http://ac.jobdu.com/problem.php?pid=1283) | [剑指Offer--035-第一个只出现一次的字符位置](http://blog.csdn.net/gatieme/article/details/51319158) | [035-第一个只出现一次的字符位置](https://github.com/gatieme/CodingInterviews/tree/master/035-第一个只出现一次的字符位置) |
1515

1616

1717

18+
19+
<br>**您也可以选择[回到目录-剑指Offer--题集目录索引](http://blog.csdn.net/gatieme/article/details/51916802)**
20+
21+
22+
23+
24+
1825
#题意
1926
-------
2027

@@ -72,8 +79,8 @@ public:
7279
{
7380
return i;
7481
}
75-
}
76-
return -1;
82+
}
83+
return -1;
7784
}
7885
};
7986
```
@@ -128,9 +135,9 @@ public:
128135
#bitmap方法-同计数法,略微有变动
129136
-------
130137

131-
我们计数数组不简单的存储计数
132-
* 只出现一次的字符会存储出现的位置
133-
* 出现多次的字符就存储标识-1
138+
我们计数数组不简单的存储计数
139+
* 只出现一次的字符会存储出现的位置
140+
* 出现多次的字符就存储标识-1
134141
因此查找数组中非-1的最小值即可
135142

136143

@@ -149,7 +156,7 @@ public:
149156
if('a' <= str[i] && str[i] <= 'z')
150157
{
151158
if(x[str[i] - 'a'] == 0)
152-
{
159+
{
153160
// 首次出现保存出现位置
154161
x[str[i] - 'a'] = i + 1;
155162
}
@@ -167,7 +174,7 @@ public:
167174
// 首次出现保存出现位置
168175
y[str[i] - 'A']= i + 1;
169176
}
170-
else
177+
else
171178
{
172179
// 出现多次, 就置标识-1
173180
y[str[i] - 'A'] = -1;
@@ -196,4 +203,4 @@ public:
196203
return res > str.size() ? -1 : res - 1;
197204
}
198205
};
199-
```
206+
```

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ COMMIT_31="剑指Offer--031-连续子数组的最大和--http://blog.csdn.net/ga
3232
COMMIT_32="剑指Offer--032-整数中1出现的次数(从1到n整数中1出现的次数)--http://blog.csdn.net/gatieme/article/details/51292339"
3333
COMMIT_33="剑指Offer--033-把数组排成最小的数--http://blog.csdn.net/gatieme/article/details/51303662"
3434
COMMIT_34="剑指Offer--034-丑数--http://blog.csdn.net/gatieme/article/details/51308037"
35-
COMMIT_35="剑指Offer--034-丑数--http://blog.csdn.net/gatieme/article/details/51308037"
35+
COMMIT_35="剑指Offer--035-第一个只出现一次的字符位置--http://blog.csdn.net/gatieme/article/details/51319158"
3636
COMMIT_36=""
3737

3838
RETURN_TOP ="<br>**您也可以选择[回到目录-剑指Offer--题集目录索引](http://blog.csdn.net/gatieme/article/details/51916802)**"
3939

4040

4141

42-
GITHUB_COMMIT=$(COMMIT_34)
42+
GITHUB_COMMIT=$(COMMIT_35)
4343

4444

4545
all:github

0 commit comments

Comments
 (0)