Skip to content

Commit eaf3e63

Browse files
authored
Merge pull request #2094 from YuuuuuuYu/main
[YuuuuuuYu] WEEK 03 solutions
2 parents f095117 + 557ca27 commit eaf3e63

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Runtime: 1ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 76.88MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ์นด๋ฐ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜
9+
* - ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฐ’์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
10+
* 1) ์ด์ „ ํ•ฉ๋ณด๋‹ค ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ๋” ํฌ๋ฉด ํ˜„์žฌ ์ˆซ์ž๋กœ sum์„ ์ดˆ๊ธฐํ™”
11+
*/
12+
class Solution {
13+
public int maxSubArray(int[] nums) {
14+
int sum = nums[0];
15+
int max = nums[0];
16+
17+
for (int i=1; i<nums.length; i++) {
18+
sum = Math.max(nums[i], sum+nums[i]);
19+
max = Math.max(max, sum);
20+
}
21+
22+
return max;
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(log n)
4+
* - n์„ 2๋กœ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฏ€๋กœ log n์— ๋น„๋ก€
5+
*
6+
* Memory: 42.32MB
7+
* Space Complexity: O(1)
8+
*
9+
* Approach: ๋น„ํŠธ ์—ฐ์‚ฐ (๋‚˜๋ˆ—์…ˆ ๋ฐฉ์‹)
10+
* 1) n์„ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€(n%2)๋ฅผ ํ™•์ธํ•˜์—ฌ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธ
11+
* 2) ๋งˆ์ง€๋ง‰์— 1์€ ๋ฌด์กฐ๊ฑด ๋‚จ๊ธฐ ๋•Œ๋ฌธ์— while๋ฌธ ์ข…๋ฃŒ ํ›„ 1์„ ๋”ํ•ด์คŒ
12+
*/
13+
class Solution {
14+
public int hammingWeight(int n) {
15+
int count = 0;
16+
while (n > 1) {
17+
count += n%2;
18+
n /= 2;
19+
}
20+
21+
return count+1;
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Runtime: 2ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 44.29MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ํˆฌ ํฌ์ธํ„ฐ
9+
* 1) ๋ฌธ์ž์—ด์˜ ์–‘ ๋์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์„ค์ •
10+
* 2) ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž๊ฐ€ ์˜์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, ํ•ด๋‹น ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
11+
*/
12+
class Solution {
13+
public boolean isPalindrome(String s) {
14+
int start = 0;
15+
int end = s.length()-1;
16+
17+
while (start < end) {
18+
char currLeft = s.charAt(start);
19+
char currRight = s.charAt(end);
20+
21+
if (!Character.isLetterOrDigit(currLeft)) {
22+
start++;
23+
} else if (!Character.isLetterOrDigit(currRight)) {
24+
end--;
25+
} else {
26+
if (Character.toLowerCase(currLeft) != Character.toLowerCase(currRight)) {
27+
return false;
28+
}
29+
30+
start++;
31+
end--;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}

0 commit comments

Comments
ย (0)