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 3f96750

Browse files
committed
优化 242. 有效的字母异位词 Java解法
1 parent 6efb90c commit 3f96750

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,24 @@ public:
9292
9393
Java:
9494
```java
95+
/**
96+
* 242. 有效的字母异位词 字典解法
97+
* 时间复杂度O(m+n) 空间复杂度O(1)
98+
*/
9599
class Solution {
96100
public boolean isAnagram(String s, String t) {
97-
98101
int[] record = new int[26];
99-
for (char c : s.toCharArray()) {
100-
record[c - 'a'] += 1;
102+
103+
for (int i = 0; i < s.length(); i++) {
104+
record[s.charAt(i) - 'a']++;
101105
}
102-
for (char c : t.toCharArray()) {
103-
record[c - 'a'] -= 1;
106+
107+
for (int i = 0; i < t.length(); i++) {
108+
record[t.charAt(i) - 'a']--;
104109
}
105-
for (int i : record) {
106-
if (i != 0) {
110+
111+
for (int count: record) {
112+
if (count != 0) {
107113
return false;
108114
}
109115
}

0 commit comments

Comments
(0)

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