반응형
https://leetcode.com/problems/string-to-integer-atoi/
String to Integer (atoi) - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
fun myAtoi(s: String): Int {
var answer = ""
var isFirstLeading = true
var signNumber = 0
for (i: Int in s.indices) {
val ch = s[i]
if (isFirstLeading && ch == ' ') {
continue
} else {
isFirstLeading = false
}
if (signNumber == 0) {
if (ch == '-') {
signNumber = -1
continue
} else if (ch == '+') {
signNumber = 1
continue
} else {
signNumber = 1
}
}
if (ch.isDigit()) {
answer += ch
} else if (signNumber != 0) {
break
}
}
return if (answer.isEmpty()) {
0
} else {
if (answer.toBigInteger() > Int.MAX_VALUE.toBigInteger()) {
if (signNumber == -1) Int.MIN_VALUE
else Int.MAX_VALUE
} else {
answer.toInt() * signNumber
}
}
}
}
https://github.com/960813/leetcode-problems/commit/5a49d454673a93ec3786ddaf625bcd342f90003e
[8] Kotlin · 960813/leetcode-problems@5a49d45
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github.com
반응형
☕️ Networking
기술 직군의 기술적인 교류, 커리어 이야기, 직군 무관 네트워킹 모두 환영합니다!
위클리 아카데미 오픈 채팅방(비밀번호: 9323)
kakaotalk: https://open.kakao.com/o/gyvuT5Yd