FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

242 · feixiangcode/algorithm@e525fa9 · GitHub

Commit e525fa9

Browse files
suke
committed
242
1 parent 01b0534 commit e525fa9

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
HashMap<Character, Integer> stat = new HashMap<>();
4+
for(int i = 0 ;i < s.length();i ++){
5+
char c = s.charAt(i);
6+
Integer count = stat.get(c);
7+
if(count == null || count == 0){
8+
stat.put(c,1);
9+
continue;
10+
}
11+
stat.put(c,count + 1);
12+
}
13+
for(int i = 0; i < t.length(); i ++){
14+
char c = t.charAt(i);
15+
Integer count = stat.get(c);
16+
if(count == null){
17+
return false;
18+
}
19+
if(count == 1){
20+
stat.remove(c);
21+
continue;
22+
}
23+
if(count > 1){
24+
stat.put(c,count - 1);
25+
}
26+
}
27+
return stat.isEmpty();
28+
}
29+
public boolean isAnagram(String s, String t) {
30+
int[] alphabet = new int[26];
31+
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
32+
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
33+
for (int i : alphabet) if (i != 0) return false;
34+
return true;
35+
}
36+
}

‎Week_02/id_121/NOTE.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# 学习笔记
1+
# 学习笔记
2+
3+
## 242
4+
- 有时候单纯的看时间复杂度,并不能很好的度量一个算法的性能,尤其是在不同数据量的情况下,第一种方法时间复杂度是2n,第二种是3n,但是整体下来,第二种方法却更快一些,使用数组代替hashmap能够更快的随机访问以外,尽量少的使用对象包装器,也会提高性能

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL