-
leetcode.com/problems/compare-version-numbers/
Compare Version Numbers - 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
Given two version numbers, version1 and version2, compare them.
Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.
Return the following:
- If version1 < version2, return -1.
- If version1 > version2, return 1.
- Otherwise, return 0.
첫번째 시도.. 정규 표현식 이용
문제점 1. 문자열로 비교하려해서 틀렸다.
문제점 2. idx 하나하나 잘라내서 1.21이랑 1.12를 1.12가 더 크다고 판단한다.
두번째 시도.. 코드가 점점 더러워지고,, 난잡해진다.. 정규표현을 쓰면 안되는건가? 에러도 난다..
Gist 초보라서,,, 같은 embed 에 계쏙 edit해서 이전코드가 다 날라가는 만행을 저질렀다..
그래도 정규표현식으로 꼭 풀고싶다.. 시도하겠다!
split으로 쪼개고 자리 수 맞춰서 비교해서 풀었다..ㅠㅠ밍
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersclass Solution: def compareVersion(self, version1: str, version2: str) -> int: version1, version2 = version1.split("."), version2.split(".") if version1 == version2: return 0 len1 , len2 = len(version1), len(version2) if len1 > len2: l = len1 - len2 version2.extend([0]*l) elif len1 < len2: l = len2 - len1 version1.extend([0]*l) for idx in range(len(version1)): if int(version1[idx]) > int(version2[idx]): return 1 if int(version1[idx]) < int(version2[idx]): return -1 return 0 + 9월 13일 추가
푸럭따 ㅠㅠ 뭐 사실 정규표현은 .0제거에만 사용했지만,,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersimport re class Solution: def cmp(self, a, b): # False - False or True - True = 0 # True - False = 1 # False - True = -1 return (a > b) - (a < b) def compareVersion(self, version1: str, version2: str) -> int: # step1 : .0 제거 # $ : 로 끝나는 것 ( 패턴 종료를 알림) # + : 하나 이상 반복 # () : 소괄호 안의 문자를 하나의 문자로 인식 # * : 앞문자가 있을수도 없을 수도 # [] : 존재함 pattern = '([.]0+)*$' version1 = re.sub(pattern,'', version1) version2 = re.sub(pattern,'', version2) # step2 : "dot(.)" 단위로 split() # 비교를 위해 dot(.) 단위로 split version1 = version1.split(".") version2 = version2.split(".") # step3: 001 같은 값을 001 -> 1로 바꿔주기 위해 int() version1 = [int(i) for i in version1] version2 = [int(i) for i in version2] # 비교 return self.cmp(version1, version2) 위지원데이터 엔지니어로 근무 중에 있으며 데이터와 관련된 일을 모두 좋아합니다!. 특히 ETL 부분에 관심이 가장 크며 데이터를 빛이나게 가공하는 일을 좋아한답니다 ✨
'2020년 > 코테' 카테고리의 다른 글
[코테 연습] Bulls and Cows (0) 2020.09.14 [코테연습] Combination Sum III (0) 2020.09.14 [코테 연습] Maximum Number of Coins You Can Get (0) 2020.09.09 [코테 연습] Largest Perimeter Triangle (0) 2020.09.09 [코테 연습] Relative Sort Array (0) 2020.09.09