Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99b8b57

Browse files
Update 0242.有效的字母异位词.md
1 parent d5b2f68 commit 99b8b57

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0242.有效的字母异位词.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,31 @@ func isAnagram(s string, t string) bool {
181181
}
182182
```
183183

184+
Go 写法二(只对字符串遍历一次)
185+
```go
186+
func isAnagram(s string, t string) bool {
187+
if len(s) != len(t) {
188+
return false
189+
}
190+
records := [26]int{}
191+
for index := 0; index < len(s); index++ {
192+
if s[index] == t[index] {
193+
continue
194+
}
195+
sCharIndex := s[index] - 'a'
196+
records[sCharIndex]++
197+
tCharIndex := t[index] - 'a'
198+
records[tCharIndex]--
199+
}
200+
for _, record := range records {
201+
if record != 0 {
202+
return false
203+
}
204+
}
205+
return true
206+
}
207+
```
208+
184209
### JavaScript:
185210

186211
```js

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /