diff --git a/Franklin/README.md b/Franklin/README.md new file mode 100644 index 0000000..801685e --- /dev/null +++ b/Franklin/README.md @@ -0,0 +1,3 @@ +I stopped updated this folder, because I switched from Python to Java on Leetcode, whereas this repo is named **python-leetcode**. + +To see the my new repo for work on Leetcode, click [here](https://github.com/franklinqin0/leetcode.git). \ No newline at end of file diff --git a/Franklin/addTwoNumbers.java b/Franklin/addTwoNumbers.java new file mode 100644 index 0000000..431c9cf --- /dev/null +++ b/Franklin/addTwoNumbers.java @@ -0,0 +1,69 @@ +/* +lc problem 2 +You are given two non-empty linked lists representing two non-negative integers. +The digits are stored in reverse order and each of their nodes contain a single digit. +Add the two numbers and return it as a linked list. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself. + +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 0 -> 8 +*/ + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +public class Solution { + // My solution + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + int carry=0; + int p = l1.val; + int q = l2.val; + int sum = p+q+carry; + carry = sum/10; sum = sum%10; + ListNode ln = new ListNode(sum); + ListNode result = ln; + l1 = l1.next; l2 = l2.next; + while (l1!=null || l2!=null) { + p = l1!=null?l1.val:0; l1 = l1!=null?l1.next:null; + q = l2!=null?l2.val:0; l2 = l2!=null?l2.next:null; + sum = p+q+carry; + carry = sum/10; sum = sum%10; + ln.next = new ListNode(sum); ln = ln.next; + } + if (carry>0) { + ln.next = new ListNode(1); ln = ln.next; + } + return result; + } + + // Editorial solution + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode dummyHead = new ListNode(0); + ListNode p = l1, q = l2, curr = dummyHead; + int carry = 0; + while (p != null || q != null) { + int x = (p != null) ? p.val : 0; + int y = (q != null) ? q.val : 0; + int sum = carry + x + y; + carry = sum / 10; + curr.next = new ListNode(sum % 10); + curr = curr.next; + if (p != null) p = p.next; + if (q != null) q = q.next; + } + if (carry > 0) { + curr.next = new ListNode(carry); + } + return dummyHead.next; + } + + // instead of doing an extra operation before the start of while loop, + // the editorial soln just used a dummyHead var and return dummyHead.next at the end. + // Pretty elegant! +} \ No newline at end of file diff --git a/Franklin/blank b/Franklin/blank deleted file mode 100644 index 8b13789..0000000 --- a/Franklin/blank +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Franklin/haha b/Franklin/haha deleted file mode 100644 index f22355f..0000000 --- a/Franklin/haha +++ /dev/null @@ -1 +0,0 @@ -lalala diff --git a/Franklin/haystack.py b/Franklin/haystack.py new file mode 100644 index 0000000..9639d5c --- /dev/null +++ b/Franklin/haystack.py @@ -0,0 +1,11 @@ +#lc problem 28 +#Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. + +class Solution(object): + def strStr(self, haystack, needle): + """ + :type haystack: str + :type needle: str + :rtype: int + """ + return haystack.find(needle) diff --git a/Franklin/isPalindrome.java b/Franklin/isPalindrome.java new file mode 100644 index 0000000..93537e3 --- /dev/null +++ b/Franklin/isPalindrome.java @@ -0,0 +1,15 @@ +/* +lc problem 9 +Determine whether an integer is a palindrome. +*/ +public class Solution { + public boolean isPalindrome(int x) { + if (x<0) return false; + int rev = 0, y = x; + while (y!=0) { + rev = rev*10 + y%10; + y /= 10; + } + return (x==rev); + } +} \ No newline at end of file diff --git a/Franklin/isPalindrome.py b/Franklin/isPalindrome.py new file mode 100644 index 0000000..fbe4920 --- /dev/null +++ b/Franklin/isPalindrome.py @@ -0,0 +1,17 @@ +# lc problem 9 +# Determine whether an integer is a palindrome. + +class Solution(object): + def isPalindrome(self, x): + """ + :type x: int + :rtype: bool + """ + if (x<0): + return False + + rev = 0; y = x + while (y!=0): + rev = rev*10 + y%10 + y = y/10 + return (x==rev) diff --git a/Franklin/maximumProduct.java b/Franklin/maximumProduct.java new file mode 100644 index 0000000..65499ec --- /dev/null +++ b/Franklin/maximumProduct.java @@ -0,0 +1,42 @@ +/* +lc contest 38, problem 1 +Given an array of integers, return the maximum product of three elements in the array. +Spec: Array length >=3, element in the range [-1000, 1000]. +*/ +public class Solution { + public int maximumProduct(int[] nums) { + int product = Integer.MIN_VALUE; + List pos = new ArrayList(); + List neg = new ArrayList(); + for (int i=0; i0) {pos.add(nums[i]);} + else {neg.add(nums[i]);} + + } + Collections.sort(pos); + Collections.sort(neg, Collections.reverseOrder()); + + // two cases + int result = Integer.MIN_VALUE; + + if (pos.size()==0) { + result = neg.get(neg.size()-1)*neg.get(neg.size()-2)*neg.get(neg.size()-3); + } else if (pos.size()==1) { + result = pos.get(pos.size()-1) * neg.get(neg.size()-1)*neg.get(neg.size()-2); + } else if (pos.size()==2) { + if (neg.size()>=2) + result = pos.get(pos.size()-1) * neg.get(neg.size()-1) * neg.get(neg.size()-2); + else if (neg.size()==1) + result = nums[0]*nums[1]*nums[2]; + } else if (pos.size()>=3) { + if (neg.size()<=1) { + result = pos.get(pos.size()-1)*pos.get(pos.size()-2)*pos.get(pos.size()-3); + } else if (neg.size()>=2) { + int neg_prod = neg.get(neg.size()-1)*neg.get(neg.size()-2); + int pos_prod = pos.get(pos.size()-2)*pos.get(pos.size()-3); + result = pos.get(pos.size()-1)*Integer.max(pos_prod,neg_prod); + } + } + return result; + } +} diff --git a/Franklin/Math/sqrt.py b/Franklin/mySqrt.py similarity index 76% rename from Franklin/Math/sqrt.py rename to Franklin/mySqrt.py index 5766991..649df78 100644 --- a/Franklin/Math/sqrt.py +++ b/Franklin/mySqrt.py @@ -1,3 +1,6 @@ +# lc problem 69 +# Compute and return the square root of x. + class Solution(object): def mySqrt(self, x): """ @@ -13,10 +16,11 @@ def mySqrt(self, x): while (low!=high-1): if (mid**2 < x): low = mid - #mid = (low+high)/2 elif (mid**2 == x): return mid elif (mid**2 >x): high = mid mid = (low+high)/2 return mid + + # This solution uses binary search, and I am thinking if Newton's method would work? diff --git a/Franklin/reverseIntDigits.py b/Franklin/reverseIntDigits.py new file mode 100644 index 0000000..92dda09 --- /dev/null +++ b/Franklin/reverseIntDigits.py @@ -0,0 +1,24 @@ +#lc problem 7 +#Reverse digits of an integer (keep the negative sign). +#The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. + +class Solution(object): + def reverse(self, x): + """ + :type x: int + :rtype: int + """ + boo = 0 + if (x<0): + boo = 1 + x = -x + x_reversed_str = str(x)[::-1] + x_reversed_int = int(x_reversed_str) + if (boo==1): + x_reversed_int = -x_reversed_int + if (abs(x_reversed_int)>2**31 -1): + return 0 + else: + return x_reversed_int + + # This solution is not optimal. Try to think of another one. diff --git a/Franklin/romanToInt.py b/Franklin/romanToInt.py new file mode 100644 index 0000000..418eedb --- /dev/null +++ b/Franklin/romanToInt.py @@ -0,0 +1,32 @@ +#lc problem 13 +#Given a roman numeral, convert it to an integer. +#Input is guaranteed to be within the range from 1 to 3999. + +class Solution(object): + def romanToInt(self, s): + """ + :type s: str + :rtype: int + """ + i = 0 + result = 0 + length = len(s) + map = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} + while (i= num2): + result += num1 + i += 1 + else: + result = result - num1 + num2 + i += 2 + else: #s[i] is the last numeral + result += num1 + # to jump out of loop + i += 1 + + return result diff --git a/Franklin/twoSum.py b/Franklin/twoSum.py new file mode 100644 index 0000000..0564559 --- /dev/null +++ b/Franklin/twoSum.py @@ -0,0 +1,29 @@ +#lc problem 1 +#Given an array of integers, return indices of the two numbers such that they add up to a specific target. + +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + array = sorted(nums) + i = 0 + j = len(array)-1 + + while (array[i]+array[j] != target): + if (array[i]+array[j] < target): + i += 1 + else: + j -= 1 + + t0 = array[i] + t1 = array[j] + + pos0 = nums.index(t0) + pos1 = nums.index(t1) + if (pos0 == pos1): + pos1 = nums[pos0+1:].index(t1) + pos1 += pos0+1 + return pos0,pos1 diff --git a/Franklin/validParenthesis.py b/Franklin/validParenthesis.py new file mode 100644 index 0000000..7fe4821 --- /dev/null +++ b/Franklin/validParenthesis.py @@ -0,0 +1,23 @@ +#lc problem 20 +#Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. +#The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. + +class Solution(object): + #solution using stack + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + mid = len(s)/2 - 1 + boo = (len(s)%2 == 0) + map = {'(':')', '[':']', '{':'}'} + stack = [] + + for c in s: + if c in map.keys(): + stack.append(c) + elif c in map.values(): + if stack ==[] or c != map[stack.pop()]: + return False + return (stack == [])