File tree Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -8,19 +8,19 @@ magazineにある文字を高速にチェックできないといけないので
8
8
magazineに存在しない文字や、消費し尽くした文字を指定されたらransomは成立しなくなるのでその時点でfalseを返せる。
9
9
falseを返さずに最後までチェックし終えたら
10
10
*/
11
+ /*
12
+ mapで`[]`を使用することで不要な初期化処理やnullチェックを削除
13
+ */
11
14
class Solution {
12
15
public:
13
16
bool canConstruct (string ransomNote, string magazine) {
14
17
map<char , int > character_count;
15
18
for (char c: magazine) {
16
- if (!character_count[c]) {
17
- character_count[c] = 0 ;
18
- }
19
19
character_count[c]++;
20
20
}
21
21
22
22
for (char c: ransomNote) {
23
- if (!character_count[c] || character_count[c] <= 0 ) {
23
+ if (character_count[c] <= 0 ) {
24
24
return false ;
25
25
}
26
26
character_count[c]--;
Original file line number Diff line number Diff line change 1
1
/*
2
2
97という定数が気持ち悪かったので、'a'に変更、それ以外はstep2と変わらず。
3
3
*/
4
+ /*
5
+ 初期化をきちんとするように修正
6
+ */
4
7
class Solution {
5
8
public:
6
9
bool canConstruct (string ransomNote, string magazine) {
7
10
int char_count[26 ];
11
+ for (int i = 0 ; i < 26 ; i++) {
12
+ char_count = 0 ;
13
+ }
8
14
9
15
for (char c: magazine) {
10
16
char_count[c - ' a' ]++;
You can’t perform that action at this time.
0 commit comments