Skip to content
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

42-xxubin04 #158

Merged
merged 2 commits into from
May 1, 2024
Merged

42-xxubin04 #158

merged 2 commits into from
May 1, 2024

Conversation

xxubin04
Copy link
Member

πŸ”— 문제 링크

66. Plus One


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

15λΆ„


✨ μˆ˜λ„ μ½”λ“œ

1. λ¬Έμ œμ΄ν•΄

image

리슀트 μ•ˆ μˆ«μžλ“€μ˜ 쑰합에 1을 λ”ν•œ 값을 λ‹€μ‹œ 리슀트 ν˜•νƒœλ‘œ 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.

예제1)
[1, 2, 3]
-> [1, 2, 4]

예제2)
[4, 3, 2, 1]
-> [4, 3, 2, 2]

예제3)
[9]
-> [1, 0]


2. μ½”λ“œ 뢄석

class Solution(object):
    def plusOne(self, digits):
        n = ""
        for i in digits:
            n += str(i)
        n = int(n) + 1
        return list(map(int, list(str(n))))
  • 리슀트 μ•ˆμ˜ μˆ«μžλ“€μ„ λ¬Έμžμ—΄ν™”ν•΄μ„œ nμ—μ„œ λͺ¨λ‘ 합쳐쀀닀.
  • 그리고 n을 μ •μˆ˜ν™”ν•˜κ³  1을 더해쀀닀.
  • n을 λ¦¬μŠ€νŠΈν™”ν•˜λ©΄ 처음 주어진 리슀트처럼 각 μžλ¦Ώμˆ˜λŒ€λ‘œ λ‚˜λˆ„μ–΄μ§„λ‹€.
  • λ‚˜λˆ„μ–΄μ§„ λ¬Έμžλ“€μ„ μ •μˆ˜λ‘œ λ°”κΏ”μ£Όκ³  λ°˜ν™˜ν•΄μ€€λ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

였늘 μ‹œν—˜μ΄ λλ‚˜μ„œ μ‰¬μš΄ 문제λ₯Ό ν’€κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€...πŸ™‡πŸ»β€β™€οΈ
λͺ©μš”일뢀터 λ‹€μ‹œ μ œλŒ€λ‘œ ν•˜κ² μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬이면 단 ν•œ μ€„λ‘œ ν•΄κ²° κ°€λŠ₯ν•˜λ„€μš” γ…‹γ…‹

class Solution(object):
    def plusOne(self, digits):
        return map(int, str(int(''.join(map(str, digits))) + 1))
  1. ''.join(map(str, digits)) -> digits 배열에 μžˆλŠ” μˆ«μžλ“€μ„ 문자둜 λ°”κΎΌ λ’€ 곡백없이 ν•˜λ‚˜μ˜ λ¬Έμžμ—΄λ‘œ 뢙이기. 즉 [1, 2, 3] -> "123"
  2. int() + 1 -> 1.μ—μ„œ λ§Œλ“  λ¬Έμžμ—΄μ„ 숫자둜 λ°”κΎΌ λ’€ 1을 더함. 즉 "123" -> 123 + 1 -> 124
  3. map(int, str()) -> 2.μ—μ„œ 1을 λ”ν•œ 숫자λ₯Ό λ‹€μ‹œ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•œ λ’€, 이λ₯Ό map을 μ΄μš©ν•΄ 각 자릿수λ₯Ό int둜 μ·¨κΈ‰ν•΄ 뢄리해 리슀트둜 λ°˜ν™˜. 즉 124 -> "124" -> [1, 2, 4]

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λŠ” 리슀트 λ’€λΆ€ν„° ν•˜λ‚˜μ”© λ΄€μŠ΅λ‹ˆλ‹€..!
9κ°€ λ‚˜μ˜€κΈ° μ „κΉŒμ§€ λ°˜λ³΅ν•΄μ„œ 0으둜 λ§Œλ“€μ–΄μ£Όλ‹€κ°€
λλ‚˜λŠ” index에 +1λ₯Ό ν•΄μ€¬μŠ΅λ‹ˆλ‹€!

λ§Œμ•½ 0번째 μΈλ±μŠ€κΉŒμ§€ 9라면, indexκ°€ -1μ—μ„œ λλ‚˜κ³ ,
-1이라면 제일 μ•žμ— 1을 μΆ”κ°€ν•΄μ€¬μŠ΅λ‹ˆλ‹€!

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        index = len(digits)-1

        while digits[index] == 9 and index >= 0:
            digits[index] = 0
            index -= 1
        
        if index == -1:
            digits.insert(0, 1)
        else:
            digits[index] += 1

        return digits

@xxubin04 xxubin04 merged commit 4bf59df into AlgoLeadMe:main May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants