-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[윤태권] Week4 문제 풀이 #430
Merged
Merged
[윤태권] Week4 문제 풀이 #430
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 시간 복잡도: O(n) | ||
* - 공차가 1인 등차수열, 등차수열의 합 공식 활용하여 기대 값을 계산 -> O(1) | ||
* - 주어진 배열을 순회하면서 각 원소의 합을 계산 -> O(n) | ||
* - 기대 값에서 실제 각 원소의 합을 빼면 정답 -> O(1) | ||
* | ||
* 공간 복잡도: O(1) | ||
* | ||
*/ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
int len = nums.length; | ||
int expectedSum = len * (len + 1) / 2; | ||
int actualSum = 0; | ||
|
||
for (int num: nums) { | ||
actualSum += num; | ||
} | ||
|
||
return expectedSum - actualSum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* 시간 복잡도: O(n) | ||
* - 정규식을 통해 Alphanumeric 만 남기기. -> O(n) | ||
* - 소문자로 변환 -> O(n) | ||
* - 투 포인터를 이용하기 때문에 -> O(n/2) | ||
* 공간 복잡도: O(n) | ||
*/ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); | ||
|
||
char[] c = s.toCharArray(); | ||
|
||
int left = 0; | ||
int right = c.length - 1; | ||
|
||
while (left < right) { | ||
if (c[left++] != c[right--]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제가 자바를 잘 몰라서 궁금한 부분인데, toCharArray()로 변경 없이 string상태로 인덱스 접근하는 방식은 불가능할까요?
명시적인 시간복잡도 감소는 없겠지만, 루프를 한 싸이클 줄일 수 있을 것 같아서요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taekwon-dev 위 의견에 덧붙이자면, toCharArray() 변경하지 않고 바로 인덱스 접근 시 공간복잡도를 N -> 1로 줄일 수 있는 효과가 있을 것으로 보입니다.
@HC-kang 말씀주신 방법은 자바에서 s.charAt(index) 와 같은 방식으로 가능합니다 :)