Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5120c30

Browse files
authoredSep 8, 2024
Merge pull request #430 from taekwon-dev/main
[윤태권] Week4 문제 풀이
2 parents 1cb001f + 16977f7 commit 5120c30

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
 

‎missing-number/taekwon-dev.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* - 공차가 1인 등차수열, 등차수열의 합 공식 활용하여 기대 값을 계산 -> O(1)
4+
* - 주어진 배열을 순회하면서 각 원소의 합을 계산 -> O(n)
5+
* - 기대 값에서 실제 각 원소의 합을 빼면 정답 -> O(1)
6+
*
7+
* 공간 복잡도: O(1)
8+
*
9+
*/
10+
class Solution {
11+
public int missingNumber(int[] nums) {
12+
int len = nums.length;
13+
int expectedSum = len * (len + 1) / 2;
14+
int actualSum = 0;
15+
16+
for (int num: nums) {
17+
actualSum += num;
18+
}
19+
20+
return expectedSum - actualSum;
21+
}
22+
}

‎valid-palindrome/taekwon-dev.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* - 정규식을 통해 Alphanumeric 만 남기기. -> O(n)
4+
* - 소문자로 변환 -> O(n)
5+
* - 투 포인터를 이용하기 때문에 -> O(n/2)
6+
* 공간 복잡도: O(n)
7+
*/
8+
class Solution {
9+
public boolean isPalindrome(String s) {
10+
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
11+
12+
char[] c = s.toCharArray();
13+
14+
int left = 0;
15+
int right = c.length - 1;
16+
17+
while (left < right) {
18+
if (c[left++] != c[right--]) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.