diff --git a/leetcode/1. two sum class.py b/leetcode/1. two sum class.py new file mode 100644 index 0000000..536337c --- /dev/null +++ b/leetcode/1. two sum class.py @@ -0,0 +1,36 @@ +""" +# 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. + + + +""" +class Solution(): + def __init__(self, nums, target): + self.nums=nums + self.target=target + + def twoSum(self, nums, target): + d = {} # dictionary + for i in range(len(nums)): + num1 = nums[i] + num2 = target - num1 + print("d1=",d) + if num2 in d: + + return [d[num2], i] + d[num1] = i + +sol = Solution([8,5], 13) # 類別 給值 命名sol +print(sol.twoSum([20,8,8,40,50,5,80], 13)) # sol 內的twoSum + +""" +# print("num1=",num1) =8 +# print("num2= 13-5 =",num2) +""" \ No newline at end of file diff --git a/leetcode/1. two sum.py b/leetcode/1. two sum.py new file mode 100644 index 0000000..71a8fba --- /dev/null +++ b/leetcode/1. two sum.py @@ -0,0 +1,42 @@ +""" +# LeetCode +# 1. Two Sum + +# num1 + num2 = target +https://ithelp.ithome.com.tw/articles/10269246 + +此題有 class Solution(object): + 須再學習 +""" + +def twoSum(nums, target): + d = {} + for i in range(len(nums)): + print("i=",i) + num1 = nums[i] + num2 = target - num1 # 5有重複 + if num2 in d: + print("num2=",num2) + print("num1=",num1) + + return [d[num2], i] + d[num1] = i + print("d[num1]=",i) + +twoSum([3,8,8,2,5,4], 13) + +# <法二> +# def twoSum( nums, target): +# i = 0 +# j = len(nums) - 1 + +# while (nums[i] + nums[j] != target): +# if (nums[i] + nums[j] > target): +# j = j - 1 +# else: +# i = i + 1 +# # print("i=",i,"j=",j) +# return [i, j] + +# twoSum([2,3,4,5,8,8], 13) +# 此法需排序 diff --git a/leetcode/13. romanToInt.py b/leetcode/13. romanToInt.py new file mode 100644 index 0000000..b61879e --- /dev/null +++ b/leetcode/13. romanToInt.py @@ -0,0 +1,83 @@ +""" +13. Roman to Integer + +I 1 +V 5 +X 10 +L 50 +C 100 +D 500 +M 1000 + +""" + +class Solution(object): + def __init__(self, s): + self.s = s + def romanToInt(self, s): + + d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} + result = 0 + + for i in range (len(s)): + + if i >0 and d[ s[i] ] > d[ s[i-1] ]: + result += d[ s[i] ] -2* d[ s[i-1] ] + + else: + result += d[ s[i] ] + return result + + + +n = Solution("MCMXCIV") +print("n=", n.romanToInt("MCMXCIV")) + +""" +MC = 1000+100 = 1100 +CM = 1000-100 = 900 +MCM = 1900 = 1000+100 -100*2 +MX = 10 +MCMX = 1910 +XC = 100-10*2 (MCMX +XC -X ) +MCMXC = 1990 +CI +MCMXCI +IV + + + + +class Solution(object): + def __init__(self, s): + self.s = s + def romanToInt(self, s): + + d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} + result = 0 + + for i in range (len(s)): + + print("i=",i) + if i >0 and d[ s[i] ] > d[ s[i-1] ]: + print("s[i] _1 = ",s[i]) + print("d[ s[i] ]=",d[ s[i] ]) + print("d[ s[i-1]]=",d[ s[i-1] ]) + print("result_1=",result) + + result += d[ s[i] ] -2* d[ s[i-1] ] + print("result_2=",result,"\n") + else: + print("s[i] _2 = ",s[i]) + + result += d[ s[i] ] + print("result_3=",result,"\n") + return result + +""" + + + + + + diff --git a/leetcode/14. Longest Common Prefix.py b/leetcode/14. Longest Common Prefix.py new file mode 100644 index 0000000..5bd8800 --- /dev/null +++ b/leetcode/14. Longest Common Prefix.py @@ -0,0 +1,42 @@ +""" +# leetcode +# 14. Longest Common Prefix + +strs[0] => flower +strs[1:] => ['flow', 'flight'] + +""" +def longestCommonPrefix(strs): + + for i in range( len(strs[0]) ): # 第一個單字的長度 + for string in strs[1:]: # 第1個~end + if i >= len(string) or string[i] != strs[0][i]: + # 當i>比較字串長度 或 flower 比較到字母不同 回傳到當下字母 + return strs[0][:i] + return strs[0] # 檢查 strs=[""] 時 不回傳null 需回傳"" 也就是第一個[""] + +strs = ["flower","flow","flight"] +#strs= [""] +longestCommonPrefix(strs) + + + + + + + + + + + +# 有 print 版 +# def longestCommonPrefix(strs): +# for i in range( len(strs[0]) ): # 第一個單字的長度 +# print("i = ",i) + +# for string in strs[1:]: # 第1個~end +# if i >= len(string) or string[i] != strs[0][i]: +# print("strs[0][:i] = ",strs[0][:i]) +# return strs[0][:i] +# print("strs[0] = ",strs[0]) + diff --git a/leetcode/20. Valid Parentheses.py b/leetcode/20. Valid Parentheses.py new file mode 100644 index 0000000..8862500 --- /dev/null +++ b/leetcode/20. Valid Parentheses.py @@ -0,0 +1,35 @@ +""" +# leetcode +# 20. Valid Parentheses + +"{[]}" => true + +""" + +class Solution(object): + def isValid(self, s): + + lookup = {"(":")", "[":"]", "{":"}" } # 字典 + stack = [] # 堆疊 + for bracket in s: + if bracket in lookup: + stack.append( bracket ) + + # 當pop() 最後一個 "(" 沒有對應 ")" + elif len(stack) == 0 or lookup[stack.pop()] != bracket: + + return False + + # s ="]" 不合if 所以 len(stack) == 0 + return len(stack) == 0 + +n = Solution("({}[])") +n.isValid("({}[])") + +""" +corresponding 對應 +Valid 有效的 +parentheses 圓括號 +Open brackets => ( +closing bracket => ) +""" \ No newline at end of file diff --git a/leetcode/21. Merge Two Sorted Lists.py b/leetcode/21. Merge Two Sorted Lists.py new file mode 100644 index 0000000..fe3a7ee --- /dev/null +++ b/leetcode/21. Merge Two Sorted Lists.py @@ -0,0 +1,44 @@ +""" +# leetcode +# 21. Merge Two Sorted Lists + +list1 = [1,2,4] +list2 = [1,3,4] + +Output: [1,1,2,3,4,4] + +Merge the two lists in a one sorted list. +""" + +class ListNode(object): + def __init__(self, val=0, next=None): + self.val = val + self.next = next +class Solution(object): + def mergeTwoLists(self, L1, L2): + curr = dummy = ListNode(0) # curr current dummy 虛擬 + + # val有文字有數字 只轉數字 + while L1 and L2: + if L1.val < L2.val: + curr.next = L1 # next 返回下一個輸入行 + L1 = L1.next + else: + curr.next = L2 + L2= L2.next + curr = curr.next + curr.next = L1 or L2 # L1結束 接 L2 + return dummy.next + +n= ListNode( [1,2,4], [1,3,4]) +print(n.val) +print(n.next) + + + + + + + + + diff --git a/leetcode/7. Reverse Integer.py b/leetcode/7. Reverse Integer.py new file mode 100644 index 0000000..fa323b2 --- /dev/null +++ b/leetcode/7. Reverse Integer.py @@ -0,0 +1,49 @@ +""" +# LeetCode +# 7. Reverse Integer + +""" +class Solution(object): + + def __init__(self, x): + self.x= x + + def reverse(self, x): + a = abs(x) + num = 0 + + # abs return an absolute value of number + # while迴圈 執行直到 a = 0 + while (a != 0): + temp = a % 10 + print("a=",a) + + num = num*10 + temp + print("temp=",temp) + + print("num=",num) + a = a//10 + # a = int (a/10) + + + if x>0 and num < 2147483647: + print("a=",a) + return num + elif x<0 and num < 2147483648: + return -num + else: + return 0 + +num = Solution(12321) +print("rev=",num.reverse(12321)) + + +""" +# if x = 12321 +# a = 1232, num = 1 +# temp = 2(餘數) +# num = 10+2 =12 +# a = 123, num = 12 +# temp = 3(餘數) +# num = 120 +3 +""" \ No newline at end of file diff --git a/leetcode/9. Palindrome.py b/leetcode/9. Palindrome.py new file mode 100644 index 0000000..602fb50 --- /dev/null +++ b/leetcode/9. Palindrome.py @@ -0,0 +1,34 @@ +""" +# LeetCode +# 9. Palindrome + +""" +class Solution(object): + def __init__(self, x): + self.x=x + def isPalindrome(self, x): + a = abs(x) + num = 0 + + while (a != 0): + temp = a % 10 + num = num*10 + temp + a = a//10 + # a = int (a/10) + + if num ==x and x>=0 and num < 2147483647: + return True + return num + elif num !=x and x<0 and num < 2147483648: + return False + else: + return False + +n = Solution(0) +print(n.isPalindrome(0)) + +# x = 0 +# x = 10 +# x = 121 + +