Python Algorithm
LeetCode
1. Two Sum
- Given an array of integers [nums] and an integer target, return indices of the two numbers such that they add up to target.
- You may assume that each input would have exactly one solution, and you may not use the same element twice.
- You can return the answer in any order.
- Constraints
- 2 ≤ nums.length ≤ 10⁴
- -10⁹ ≤ nums[i] ≤ 10⁹
- -10⁹ ≤ target ≤ 10⁹
- Only one valid answer exists.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {}
for i, num in enumerate(nums):
diff = target - num
if diff in d:
return [d[diff], i]
d[num] = i
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
length = len(nums)
for i, num in enumerate(nums):
for j in range(i+1, length):
if num + nums[j] == target:
return [i, j]
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
13. Roman to Integer
- Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
| Symbol |
Value |
| I |
1 |
| V |
5 |
| X |
10 |
| L |
50 |
| C |
100 |
| D |
500 |
| M |
1000 |
- For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
- Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
- I can be placed before V (5) and X (10) to make 4 and 9.
- X can be placed before L (50) and C (100) to make 40 and 90.
- C can be placed before D (500) and M (1000) to make 400 and 900.
- Given a roman numeral, convert it to an integer.
- Constraints
- 1 ≤ s.length ≤ 15
- s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
- It is guaranteed that s is a valid roman numeral in the range [1, 3999].
class Solution:
def romanToInt(self, s: str) -> int:
d = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50,
'XC': 90, 'C': 100, 'CD': 400, 'D': 500, 'CM': 900, 'M': 1000}
keys = sorted(d.keys(), key=lambda x: d[x], reverse=True)
answer = 0
while s:
for key in keys:
if s.startswith(key):
# answer에 해당 숫자만큼 더해주고
# s에서 해당 문자열을 잘라내 주기
answer += d[key]
s = s[len(key):]
break
return answer
class Solution:
def romanToInt(self, s: str) -> int:
d = {'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50,
'XC': 90, 'C': 100, 'CD': 400, 'D': 500, 'CM': 900, 'M': 1000}
i = 0
answer = 0
while i < len(s):
if s[i:i+2] in d:
answer += d[s[i:i+2]]
i += 2
else:
answer += d[s[i]]
i += 1
return answer
class Solution:
def romanToInt(self, s: str) -> int:
roman_num = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
answer = roman_num[s[0]]
for i in range(1, len(s)):
if roman_num[s[i]] - roman_num[s[i-1]] > 0:
answer += (roman_num[s[i]] - 2 * roman_num[s[i-1]])
else:
answer += roman_num[s[i]]
return answer
https://leetcode.com/problems/two-sum/
https://leetcode.com/problems/roman-to-integer/