Create 125. Valid Palindrome.md#5
Conversation
| } | ||
| if( std::tolower(s[start_index]) != std::tolower(s[end_index]) ) { | ||
| return false; | ||
| } else { |
There was a problem hiding this comment.
私は、この else はなくして、中身を下に下げます。インデントを深くしたくないためです。
| int start_index = 0; //from_start?, start_to_end_index, | ||
| int end_index = s.length() - 1; | ||
|
|
||
| while(start_index <= end_index) { |
| --end_index; | ||
| continue; | ||
| } | ||
| if( std::tolower(s[start_index]) != std::tolower(s[end_index]) ) { |
There was a problem hiding this comment.
C++はあまり触っていないので,間違いがあるかもしれませんが,こちらの方法はいかがでしょうか.
if( std::tolower(s[start_index]) == std::tolower(s[end_index]) ) {
++start_index;
--end_index;
continue
}
return False参考
https://github.com/t-ooka/leetcode/pull/6/files
のstep4です.
|
|
||
| vector<char> transformed_string; // normalized, standar~ | ||
|
|
||
| for(char c: s) { |
There was a problem hiding this comment.
キーワードのあとに空白を空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
if (b) { // Space after the keyword in conditions and loops.
また、 : の前後に空白を空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
// Range-based for loops always have a space before and after the colon.
for (auto x : counts) {
| vector<char> transformed_string; // normalized, standar~ | ||
|
|
||
| for(char c: s) { | ||
| if('a' <= c && c <= 'z'){ |
There was a problem hiding this comment.
{ の前に空白を空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
void f(bool b) { // Open braces should always have a space before them.
|
|
||
| for(char c: s) { | ||
| if('a' <= c && c <= 'z'){ | ||
| transformed_string.push_back(c); |
There was a problem hiding this comment.
std::tolower() は 'a'~'z'、'0'~'9' に対しては、入力をそのまま返します。これを利用し、 if 文をまとめてしまったほうが読みやすくなると思います。
for (char c : s) {
if (isalnum(c)) {
transformed_string.push_back(std::tolower(c));
}
}
| transformed_string.push_back(c); | ||
| }else if('A' <= c && c <= 'Z') { | ||
| transformed_string.push_back(std::tolower(c)); | ||
| continue; |
There was a problem hiding this comment.
if else 文で continue が入っているのと入っていないのとで一貫性が取れていない点が気になりました。この書き方であれば、 continue はすべて省き、 else 文を消したほうが読みやすくなると思います。
|
|
||
| int n = transformed_string.size(); | ||
| for(int i = 0; i < n / 2; ++i) { | ||
| if(transformed_string[i] == transformed_string[n - 1 - i]) { |
There was a problem hiding this comment.
条件を反転して early return したほうが読みやすく感じます。
for (int i = 0; i < n / 2; ++i) {
if (transformed_string[i] != transformed_string[n - 1 - i]) {
return false;
}
}
| } else { | ||
| return false; | ||
| } | ||
| } |
| int end_index = s.length() - 1; | ||
|
|
||
| while(start_index <= end_index) { | ||
| if( !isalnum(s[start_index]) ) { |
There was a problem hiding this comment.
() の内側に空白を空けても大丈夫だと思うのですが、他の箇所と一貫性を取る事をお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Horizontal_Whitespace
// Loops and conditions may have spaces inside parentheses, but this
// is rare. Be consistent.
switch ( i ) {
if ( test ) {
for ( int i = 0; i < 5; ++i ) {`
| ++start_index; | ||
| continue; | ||
| } | ||
| if( !isalnum(s[end_index]) ) { |
There was a problem hiding this comment.
ここはifではなく、whileにして、ブロック内のcontinueをなくすと良さそうです。
コードを読む時に親ブロックのwhileまで戻らずに済むため、読みやすくなります。
| continue; | ||
| } else { | ||
| // c が英数字以外の場合の処理 | ||
| continue; |
There was a problem hiding this comment.
https://github.com/kzhra/Grind41/pull/5/files#r1667239326
の指摘と被りますが
あってもなくても挙動がおなじになるcontinueが入っているのが気になりました。
書くべき理由がなさそうなので、消したほうが読みやすくなりそうです。
https://leetcode.com/problems/valid-palindrome/description/