FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Leetcode_262_115_java.java at master · yanlingli3799/algorithm · GitHub
yanlingli3799
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Leetcode_262_115_java.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (65 loc) · 2.15 KB
Breadcrumbs
algorithm
/
Leetcode_262_115_java.java
Copy path
File metadata and controls
73 lines (65 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package
data
.
leetcode
.
hash
;
import
java
.
util
.
HashMap
;
public
class
Leetcode262
{
/**
* 执行时间过长
* 执行用时 : 57 ms, 在Valid Anagram的Java提交中击败了15.39% 的用户
* 内存消耗 : 41.9 MB, 在Valid Anagram的Java提交中击败了25.98% 的用户
*
* @param s
* @param t
* @return
*/
public
static
boolean
isAnagram
(
String
s
,
String
t
) {
if
(
s
.
length
() !=
t
.
length
())
return
false
;
HashMap
<
Character
,
Integer
>
sMap
=
new
HashMap
<
Character
,
Integer
>();
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
char
c
=
s
.
charAt
(
i
);
Integer
integer
=
sMap
.
get
(
c
);
if
(
integer
==
null
) {
integer
=
0
;
}
sMap
.
put
(
c
, ++
integer
);
}
for
(
int
i
=
0
;
i
<
t
.
length
();
i
++) {
if
(
sMap
.
containsKey
(
t
.
charAt
(
i
))) {
Integer
integer
=
sMap
.
get
(
t
.
charAt
(
i
));
integer
--;
if
(
integer
==
0
) {
sMap
.
remove
(
t
.
charAt
(
i
));
}
else
{
sMap
.
put
(
t
.
charAt
(
i
),
integer
);
}
}
else
{
return
false
;
}
}
return
true
;
}
/**
* 第二种解法,是将数据进行一个因射到26位字母
* <p>
* 执行用时 : 4 ms, 在Valid Anagram的Java提交中击败了96.25% 的用户
* 内存消耗 : 38.1 MB, 在Valid Anagram的Java提交中击败了95.44% 的用户
* 进行下一个挑战:
*/
public
static
boolean
isAnagram2
(
String
s
,
String
t
) {
if
(
s
.
length
() !=
t
.
length
())
return
false
;
int
[]
freq1
=
new
int
[
26
];
int
[]
freq2
=
new
int
[
26
];
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++)
freq1
[
s
.
charAt
(
i
) -
'a'
]++;
for
(
int
i
=
0
;
i
<
t
.
length
();
i
++)
freq2
[
t
.
charAt
(
i
) -
'a'
]++;
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
freq1
[
i
] !=
freq2
[
i
])
return
false
;
}
return
true
;
}
public
static
void
main
(
String
[]
args
) {
isAnagram2
(
"aacc"
,
"ccac"
);
//false
isAnagram2
(
"aacc"
,
"caac"
);
//true
}
}
Back
|
FazBrowse Home
|
New Git URL