Skip to content

Commit 61fa050

Browse files
authored
Merge pull request #719 from forest000014/main
[forest000014] Week 2
2 parents b93073d + 0bd7993 commit 61fa050

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
i๋ฒˆ์งธ ์นธ์— ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์€ (1) i-2๋ฒˆ์งธ ์นธ์—์„œ 2์นธ์„ ์ ํ”„ํ•˜๊ฑฐ๋‚˜ (2) i-1๋ฒˆ์งธ ์นธ์—์„œ 1์นธ์„ ์ ํ”„ํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ฟ์ž…๋‹ˆ๋‹ค. (MECEํ•จ)
3+
๋”ฐ๋ผ์„œ, (i๋ฒˆ์งธ ์นธ์— ๊ฐ€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜) = (i-2๋ฒˆ์งธ ์นธ์— ๊ฐ€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜) + (i-1๋ฒˆ์งธ ์นธ์— ๊ฐ€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜)
4+
5+
Runtime: 0 ms (Beats: 100.00%)
6+
Time Complexity: O(n)
7+
8+
Memory: 40.47 MB (Beats: 36.79%)
9+
Space Complexity: O(1)
10+
*/
11+
12+
class Solution {
13+
public int climbStairs(int n) {
14+
if (n == 1) {
15+
return 1;
16+
} else if (n == 2) {
17+
return 2;
18+
} else {
19+
int prev2 = 1;
20+
int prev1 = 2;
21+
int cur = 0;
22+
for (int i = 3; i <= n; i++) {
23+
cur = prev2 + prev1;
24+
prev2 = prev1;
25+
prev1 = cur;
26+
}
27+
return cur;
28+
}
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
s์™€ t๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ, ์นด์šดํŒ…์„ ์œ„ํ•ด 26๊ฐœ์˜ ๊ณ ์ •๋œ key๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๊ณ , ๋ฐฐ์—ด์ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ณ  ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•จ
3+
4+
Runtime: 4 ms (Beats: 76.59%)
5+
Time Complexity: O(n)
6+
7+
Memory: 43.04 MB (Beats: 78.65%)
8+
Space Complexity: O(1)
9+
*/
10+
11+
class Solution {
12+
public boolean isAnagram(String s, String t) {
13+
if (s.length() != t.length())
14+
return false;
15+
16+
int[] cnt = new int[26];
17+
18+
for (int i = 0; i < s.length(); i++) {
19+
cnt[s.charAt(i) - 'a']++;
20+
}
21+
for (int i = 0; i < t.length(); i++) {
22+
cnt[t.charAt(i) - 'a']--;
23+
}
24+
25+
for (int i = 0; i < 26; i++) {
26+
if (cnt[i] != 0)
27+
return false;
28+
}
29+
30+
return true;
31+
}
32+
}

0 commit comments

Comments
ย (0)