You are given a string number representing a positive integer and a character digit.
Return the resulting string after removing exactly one occurrence of digit from number such that the value of the resulting string in decimal form is maximized. The test cases are generated such that digit occurs at least once in number.
한글요약
number에 들어있는 숫자가 문자열로 표현되어있습니다.
digit에는 0-9 까지 한글자만 들어있는 문자열입니다.
number문자열에서 digit과 동일한 문자열이 있으면 한글자만 빼고 출력합니다.
만약 number에 digit과 같은 문자열이 두개면
각각 뺀 문자열을 수크기로 비교해서 더 큰 값을 문자열로 반환합니다.
Input: number = "123", digit = "3"
Output: "12"
Input: number = "1231", digit = "1"
Output: "231"
Since 231 > 123, we return "231".
Input: number = "551", digit = "5"
Output: "51"
제한사항
- 2 ≤ number.length ≤ 100
- number consists of digits from '1' to '9'.
- digit is a digit from '1' to '9'.
- digit occurs at least once in number.
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
num = list(number)
ans = -1
for i,n in enumerate(num):
if n == digit:
tmp = deepcopy(num)
tmp.pop(i)
ans = max(int(''.join(tmp)), ans)
return str(ans)
class Solution(object):
def removeDigit(self, number, digit):
answer= '0'
for i in range(len(number)):
if(number[i]==digit):
if(answer < number[:i]+number[i+1:]):
answer = number[:i]+number[i+1:]
return answer
파이써닉하게 풀었네요.. 쉬운문제긴 하지만 배울 게 있었습니다.