Conversation
Combination Sum
Best Time to Buy and Sell Stock
Invert Binary Tree
Binary Search
…search_tree Lowest Common Ancestor of a Binary Search Tree
Linked List Cycle
Implement Queue using Stacks
Ransom Note
Majority Element
nodchip
reviewed
Oct 15, 2024
| 後ろから見ていく方法は追加の空間が必要なくなるが、実装が複雑になる。 | ||
| 個人的には必要がなければできるだけスタックを使ってシンプルに実装する方が良いと思っている。 | ||
| */ | ||
| func backspaceCompareStep1(s string, t string) bool { |
There was a problem hiding this comment.
これが一番いいように思いますね。変わったことをしたいならば Goroutine はどうですか。
func iterateReverse(s []rune, c chan rune) {
defer close(c)
backspaceCount := 0
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '#' {
backspaceCount++
} else if backspaceCount > 0 {
backspaceCount--
} else {
c <- s[i]
}
}
}
func backspaceCompare(s string, t string) bool {
cs := make(chan rune)
ct := make(chan rune)
go iterateReverse([]rune(s), cs)
go iterateReverse([]rune(t), ct)
for vs := range cs {
vt, ok := <-ct
if !ok || vs != vt {
return false
}
}
if _, ok := <-ct; ok {
return false
}
return true
}```| i, j := len(runeS)-1, len(runeT)-1 | ||
| for i >= 0 || j >= 0 { | ||
| i, j = nextIndex(runeS, i), nextIndex(runeT, j) | ||
| if i >= 0 && j >= 0 && runeS[i] == runeT[j] { |
There was a problem hiding this comment.
i >= 0 && j >= 0 が 2 回登場しているなど、ループ内の処理の流れの見通しが悪く感じました。処理の順番を変えたほうがすっきりすると思います。
自分は Go 言語を書けないため、 C++ で書きますが、
class Solution {
public:
bool backspaceCompare(string s, string t) {
int s_index = ProcessBackspace(s.size() - 1, s);
int t_index = ProcessBackspace(t.size() - 1, t);
while (s_index >= 0 && t_index >= 0) {
if (s[s_index] != t[t_index]) {
return false;
}
s_index = ProcessBackspace(s_index - 1, s);
t_index = ProcessBackspace(t_index - 1, t);
}
return s_index < 0 && t_index < 0;
}
int ProcessBackspace(int index, const string& s) {
int num_backspaces = 0;
while (index >= 0) {
if (s[index] == '#') {
++num_backspaces;
--index;
continue;
}
if (num_backspaces > 0) {
--num_backspaces;
--index;
continue;
}
break;
}
return index;
}
};
はいかがでしょうか?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backspace String Compareを解きました。レビューをお願い致します。
問題:https://leetcode.com/problems/backspace-string-compare/
言語:Go
すでに解いている方々:
Kitaken0107/GrindEasy#21