#12915
๐ Question ?
https://school.programmers.co.kr/learn/courses/30/lessons/12915
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String> {
var answer = arrayOf<String>()
return answer
}
}
๐งฉ Thought Process
- for๋ฌธ ๋๋ ค์ n๋ฒ์งธ ์ธ๋ฑ์ค์ ์ํ๋ฒณ ๊ฐ์ ธ์ค๊ธฐ
- ์ฌ์ ์์ผ๋ก ์์ ๋ฌธ์์ด์ด ์์ชฝ์ ์์นํ๋๋ก ํ๋ ๋ฉ์๋ ๋ง๋ค๊ธฐ
- for๋ฌธ - ์ํ๋ฒณ์ ์์คํค ์ฝ๋๊ฐ์ ๋น๊ตํ์ฌ ์ค๋ฆ์ฐจ์์ผ๋ก ๋ฐ๊พธ๊ธฐ
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String> {
var answer = arrayOf<String>()
return answer
}
fun dictionaryOrder(strings: Array<String>, n:Int): mutableList<String> {
for (i in strings.indices) {
var charList1 = string[i].toCharArray().toMutableListOf<Char>()
var charList2 = string[i+1].toCharArray().toMutableListOf<Char>()
for (j in strings[i].toCharArray().indices) {
if (j == n) {
continue
} else if (j == n - 1) {
charList2 = string[i+2].toCharArray().toMutableListOf<Char>()
}
if (charList1[j] == charList2[j]) {
continue
}
}
}
}
}
๋ญ๊ฐ ๋ง ๋์ ์ด๋๋ฐ ๋๋ฌด ๋์ด ์๋ ๋๋์ด๋ค. ๋ํ ์ผ์ด ๋ง์ ๋ฌธ์ ์ธ๊ฑฐ ๊ฐ๋ค...
๋ค๋ฅธ์ฌ๋ ํ์ด
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String>
= strings.sortedBy { it[n] }.toTypedArray()
}
์ถ์ฒ:https://yenne.tistory.com/m/93
์ฌ์ ์ ์ ๋ ฌ์ด ์๋๋ ์ฝ๋์ด๊ธด ํ์ง๋ง sortedBy ํจ์๋ฅผ ์ดํดํ๊ธฐ ์ข์๊ฑฐ ๊ฐ์๋ณด์ฌ์ ๋ฐ๋ก ๊ธฐ๋กํด ๋จ๋ค.
ํ์ฐธ ์ฐพ์๋ณด๊ณ ์ฝ๋ ์ฝ์ด๋ณด๊ณ ๋๋์ด sort ๋ฉ์๋๋ฅผ ๋์ถฉ ์ดํดํ ๊ฒ ๊ฐ๋ค.
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String>
= strings.sorted().sortedBy { it[n] }.toTypedArray()
}
์ด ์ฝ๋๋ ์ฌ์ ์์ผ๋ก ์ ๋ ฌํ ํ์ ๋ค์ n๋ฒ์งธ ์ธ๋ฑ์ค์ ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌํ ์ฝ๋๋ค
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String> {
return strings.also {
it.sort()
it.sortBy { it[n] }
}
}
}
์ ๋ง์ด์ง it์ ์ด์ฉํ ์ฝ๋๊ฐ ๋๋ฌด ๋ํดํ๊ฒ ๋ฐ์๋ค์ฌ์ง๋ค. ๋ณผ๋๋ง๋ค ์๋ก์...
์ ์์์๋ also (scoping function) ๋ฅผ ์ฌ์ฉํ๊ฑฐ ๊ฐ๋ค. ์ฝํ๋ฆฐ์ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์๋ ํจ์๋ค์ด๋ผ๊ณ ํ๋ค.
๋ด์ผ ๊ณต๋ถํด๋ณด๋๊ฑธ๋ก... https://medium.com/@limgyumin/%EC%BD%94%ED%8B%80%EB%A6%B0-%EC%9D%98-apply-with-let-also-run-%EC%9D%80-%EC%96%B8%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EA%B0%80-4a517292df29
ํ์ด ํ๊ฐ์ง ๋ ์์
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String> {
var answer = strings
var list = answer.sortedWith(compareBy({ it[n] }, { it }))
return list.toTypedArray()
}
}
๐ Answer
class Solution {
fun solution(strings: Array<String>, n: Int): Array<String> {
var answer = strings
var list = answer.sortedWith(compareBy({ it[n] }, { it }))
return list.toTypedArray()
}
}
๐ Result
๐ Comment
sort ๋ถ๋ถ ์ ๋๋ก ๊ณต๋ถํ ๊ฑฐ ๊ฐ๋ค. ๊ทธ๋๋ ์์ง ์ข ๋ํดํ๋ค
'Algorithm > Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ #68644 (2) | 2023.12.04 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] K๋ฒ์งธ์ #42748 (0) | 2023.12.01 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด #81301 (0) | 2023.11.29 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ์์ ์ํธ #12926 (0) | 2023.11.28 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ์ต์์ง์ฌ๊ฐํ #86491 (1) | 2023.11.27 |