-
-
Notifications
You must be signed in to change notification settings - Fork 195
[eunhwa99] Week 03 Solutions #1280
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
// 시간 복잡도: DP -> O(N) | ||
// 공간 복잡도: nums 배열 크기 - O(N) | ||
|
||
// 이전 솔루션과 동일 | ||
// 시간 복잡도: O(n) - n은 주어진 배열의 길이 | ||
// 공간 복잡도: O(1) - 상수 공간 사용 | ||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int currentSum = nums[0]; | ||
int maxSum = currentSum; | ||
for (int i = 1; i < nums.length; ++i) { | ||
currentSum = Math.max(currentSum + nums[i], nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
|
||
return maxSum; | ||
|
||
public int maxSubArray(int[] nums) { | ||
int currentSum = nums[0]; | ||
int maxSum = currentSum; | ||
for (int i = 1; i < nums.length; ++i) { | ||
currentSum = Math.max(currentSum + nums[i], nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
|
||
return maxSum; | ||
} | ||
} | ||
|
This file contains hidden or 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,13 @@ | ||
// 시간 복잡도 O(log n) - n은 주어진 정수 | ||
// 공간 복잡도 O(1) - 상수 공간 사용 | ||
class Solution{ | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n != 0) { | ||
count += (n & 1); // 마지막 비트가 1인지 확인 | ||
n >>= 1; // 오른쪽으로 비트 이동 | ||
} | ||
return count; | ||
} | ||
} | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -1,23 +1,51 @@ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
StringBuilder str = new StringBuilder(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (Character.isLetterOrDigit(c)) { | ||
str.append(Character.toLowerCase(c)); | ||
} | ||
} | ||
|
||
int left = 0, right = str.length() - 1; | ||
while (left < right) { | ||
if (str.charAt(left) != str.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
|
||
public boolean isPalindrome(String s) { | ||
StringBuilder str = new StringBuilder(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (Character.isLetterOrDigit(c)) { | ||
str.append(Character.toLowerCase(c)); | ||
} | ||
} | ||
|
||
int left = 0, right = str.length() - 1; | ||
while (left < right) { | ||
if (str.charAt(left) != str.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
// 시간 복잡도 O(n) - 문자열 길이 n | ||
// 공간 복잡도 O(1) | ||
class newSolution { | ||
public boolean isPalindrome(String s) { | ||
int left = 0, right = s.length() - 1; | ||
|
||
while (left < right) { | ||
// 알파벳/숫자가 아닌 경우 skip | ||
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { | ||
left++; | ||
} | ||
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { | ||
right--; | ||
} | ||
|
||
// 소문자로 비교 | ||
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { | ||
return false; | ||
} | ||
|
||
left++; | ||
right--; | ||
} | ||
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.
Uh oh!
There was an error while loading. Please reload this page.