-
-
Notifications
You must be signed in to change notification settings - Fork 195
[이유진] Week1 #674
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
[이유진] Week1 #674
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package leetcode_study | ||
|
||
class SolutionContainsDuplicate { | ||
fun containsDuplicate(nums: IntArray): Boolean { | ||
val size = nums.size | ||
val numsToSet = nums.toSet() | ||
|
||
return size != numsToSet.size | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package leetcode_study | ||
class SolutionLongestConsecutiveSequence { | ||
fun longestConsecutive(nums: IntArray): Int { | ||
nums.sort() | ||
var cnt = 0 | ||
var maxCnt = 0 | ||
nums.forEachIndexed { i, _ -> | ||
if (i == 0) { | ||
cnt = 1 | ||
} | ||
else if (nums[i-1] == nums[i] - 1) { | ||
cnt++ | ||
} | ||
else if (nums[i - 1] == nums[i]) { | ||
return@forEachIndexed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 kotlin 을 잘 몰라서 질문드립니다 😅 이 부분은 어떠한 작업인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와, 신기한 코드네요 ㅋㅋ 저도 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. forEach 문으로 return 해줍니다~ |
||
} else { | ||
cnt = 1 | ||
} | ||
maxCnt = maxOf(maxCnt, cnt) | ||
} | ||
|
||
return maxCnt | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package leetcode_study | ||
class SolutionTopKFrequentElements { | ||
fun topKFrequent(nums: IntArray, k: Int): IntArray { | ||
val numsCount = mutableMapOf<Int, Int>() | ||
for (num in nums) { | ||
val value = numsCount.getOrDefault(num, 0) | ||
numsCount[num] = value + 1 | ||
} | ||
val sortedNumsCount = numsCount.entries | ||
.sortedByDescending { it.value } | ||
.associate { it.toPair() } | ||
|
||
return sortedNumsCount.keys.take(k).toIntArray() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package leetcode_study | ||
|
||
class SolutionValidPalindrome { | ||
fun isPalindrome(s: String): Boolean { | ||
val sToCharArray = s.toCharArray() | ||
var startIndex = 0 | ||
var endIndex = sToCharArray.size - 1 | ||
while (startIndex < endIndex) { | ||
if (!sToCharArray[startIndex].isLetterOrDigit() || sToCharArray[startIndex].isWhitespace()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 글자 부분인지 확인하는 if문으로 보이는데요~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흠... 🤔 코드는 좀 더 복잡해도 저는 현재 방법이 더 효율적일 수 있다는 생각이 들어요. 앞단에서 특수문자, 공백을 제거하려면 입력 문자열을 2번 스캔해야하니까요. 물론 대세(빅오 표현법 상으로는)에는 지장이 없겠지만요 😄 |
||
startIndex++ | ||
continue | ||
} | ||
if (!sToCharArray[endIndex].isLetterOrDigit() || sToCharArray[endIndex].isWhitespace()) { | ||
endIndex-- | ||
continue | ||
} | ||
if (sToCharArray[startIndex].lowercase() == sToCharArray[endIndex].lowercase()) { | ||
startIndex++ | ||
endIndex-- | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} |
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.
안녕하세요 ^^
이 부분은 Set으로 중복 값을 제거하신거군요!
풀이가 간결해서 좋아보입니다 👍