質問編集履歴
4
changed code
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,9 +13,6 @@
|
|
13
13
|
class Solution {
|
14
14
|
public:
|
15
15
|
int firstUniqChar(string s) {
|
16
|
-
|
17
|
-
int size=(int)(s.length());
|
18
|
-
bool repeat;
|
19
16
|
|
20
17
|
int size=(int)(s.length());
|
21
18
|
bool repeat;
|
3
changed code
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
}
|
37
37
|
}
|
38
38
|
return -1;
|
39
|
-
int nonRepeatedNum;
|
39
|
+
// int nonRepeatedNum;
|
40
40
|
// if(size==1){
|
41
41
|
// return 0;
|
42
42
|
// }else if(size==0){
|
2
コードの変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -36,61 +36,61 @@
|
|
36
36
|
}
|
37
37
|
}
|
38
38
|
return -1;
|
39
|
-
|
39
|
+
int nonRepeatedNum;
|
40
|
-
|
40
|
+
// if(size==1){
|
41
|
-
|
41
|
+
// return 0;
|
42
|
-
|
42
|
+
// }else if(size==0){
|
43
|
-
|
43
|
+
// return -1;
|
44
|
-
|
44
|
+
// }
|
45
45
|
|
46
|
-
|
46
|
+
// for(int i=0;i<=size;i++){
|
47
47
|
|
48
|
-
|
48
|
+
// repeat=false;
|
49
|
-
|
49
|
+
// nonRepeatedNum=-1;
|
50
50
|
|
51
|
-
|
51
|
+
// if(i!=size-1){
|
52
|
-
|
52
|
+
// //上り(要素数の小さい方から大きい方へと比較していく)
|
53
|
-
|
53
|
+
// for(int j=i+1;j<size;j++){
|
54
|
-
|
54
|
+
// if(s[i]==s[j]){
|
55
|
-
|
55
|
+
// repeat=true;
|
56
|
+
// }
|
56
|
-
|
57
|
+
// }
|
57
|
-
}
|
58
58
|
|
59
|
-
|
59
|
+
// //下り(要素数の大きい方から小さい方へと比較していく
|
60
|
-
|
60
|
+
// if(i>0){
|
61
|
-
|
61
|
+
// for(int k=i-1;k>=0;k--){
|
62
62
|
|
63
|
-
|
63
|
+
// if(s[i]==s[k]){
|
64
|
-
|
64
|
+
// repeat=true;
|
65
|
+
// }
|
66
|
+
// }
|
65
|
-
|
67
|
+
// }
|
66
|
-
}
|
67
|
-
}
|
68
|
-
|
68
|
+
// //最後の文字から、要素数の小さい方へと比較していく
|
69
|
-
|
69
|
+
// }else if(i==size-1){
|
70
|
-
|
70
|
+
// for(int m=size-2;m>=0;m--){
|
71
|
-
|
71
|
+
// if(s[i]==s[m]){
|
72
|
-
|
72
|
+
// //cout<<"last 1"<<endl;
|
73
|
-
|
73
|
+
// repeat=true;
|
74
|
+
// //最後の2文字を後ろから比較したから、
|
75
|
+
// //iにsizeを
|
76
|
+
// //代入して、ループを終わらせる
|
77
|
+
// i=size;
|
78
|
+
// }
|
74
|
-
|
79
|
+
// }
|
75
|
-
//iにsizeを
|
76
|
-
|
80
|
+
// }
|
77
|
-
i=size;
|
78
|
-
}
|
79
|
-
}
|
80
|
-
}
|
81
81
|
|
82
|
-
|
82
|
+
// if(nonRepeatedNum==-1 && repeat==false){
|
83
|
-
|
83
|
+
// //つまり、一つ目の、繰り返しのなかった数字
|
84
|
-
|
84
|
+
// nonRepeatedNum=i;
|
85
|
-
|
85
|
+
// break;
|
86
|
+
// }
|
86
|
-
|
87
|
+
// }
|
87
|
-
}
|
88
88
|
|
89
|
-
|
89
|
+
// if(nonRepeatedNum==-1){
|
90
|
-
|
90
|
+
// return -1;
|
91
|
-
|
91
|
+
// }else{
|
92
|
-
|
92
|
+
// return nonRepeatedNum;
|
93
|
-
|
93
|
+
// }
|
94
94
|
}
|
95
95
|
};
|
96
96
|
```
|
1
自分のコードを見返していたらコードを短くする方法を見つけました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -16,7 +16,27 @@
|
|
16
16
|
|
17
17
|
int size=(int)(s.length());
|
18
18
|
bool repeat;
|
19
|
+
|
20
|
+
int size=(int)(s.length());
|
21
|
+
bool repeat;
|
22
|
+
|
23
|
+
for(int i=0;i<size;i++){
|
24
|
+
|
25
|
+
repeat=false;
|
26
|
+
|
27
|
+
for(int j=0;j<size;j++){
|
28
|
+
|
29
|
+
if(i!=j){
|
30
|
+
if(s[i]==s[j])
|
31
|
+
repeat=true;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
if(repeat==false){
|
35
|
+
return i;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return -1;
|
19
|
-
int nonRepeatedNum;
|
39
|
+
~~int nonRepeatedNum;
|
20
40
|
if(size==1){
|
21
41
|
return 0;
|
22
42
|
}else if(size==0){
|
@@ -70,7 +90,7 @@
|
|
70
90
|
return -1;
|
71
91
|
}else{
|
72
92
|
return nonRepeatedNum;
|
73
|
-
}
|
93
|
+
}~~
|
74
94
|
}
|
75
95
|
};
|
76
96
|
```
|