From cefec1b61ec6948d3bddae88c80ca11b643fad0a Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:17:16 -0400 Subject: [PATCH 01/34] Create romanToInt --- Franklin/romanToInt | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Franklin/romanToInt diff --git a/Franklin/romanToInt b/Franklin/romanToInt new file mode 100644 index 0000000..418eedb --- /dev/null +++ b/Franklin/romanToInt @@ -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 From 53dca7d1f676abf1a456101a59aedfa752766354 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:17:27 -0400 Subject: [PATCH 02/34] Delete blank --- Franklin/blank | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Franklin/blank diff --git a/Franklin/blank b/Franklin/blank deleted file mode 100644 index 8b13789..0000000 --- a/Franklin/blank +++ /dev/null @@ -1 +0,0 @@ - From ec551467605998e10ad5998393b51af899370385 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:17:33 -0400 Subject: [PATCH 03/34] Delete haha --- Franklin/haha | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Franklin/haha diff --git a/Franklin/haha b/Franklin/haha deleted file mode 100644 index f22355f..0000000 --- a/Franklin/haha +++ /dev/null @@ -1 +0,0 @@ -lalala From 6ca2554272d7490deceb6a22c5a19748dbffadc3 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:19:39 -0400 Subject: [PATCH 04/34] Create mySqrt --- Franklin/mySqrt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Franklin/mySqrt diff --git a/Franklin/mySqrt b/Franklin/mySqrt new file mode 100644 index 0000000..e97e5af --- /dev/null +++ b/Franklin/mySqrt @@ -0,0 +1,24 @@ +# lc problem 69 +# Compute and return the square root of x. + +class Solution(object): + def mySqrt(self, x): + """ + :type x: int + :rtype: int + """ + #corner case + if (x==1): + return 1 + low = 0 + high = x + mid = (low+high)/2 + while (low!=high-1): + if (mid**2 < x): + low = mid + elif (mid**2 == x): + return mid + elif (mid**2 >x): + high = mid + mid = (low+high)/2 + return mid From d01d10a1a331a9eb8a38b168c83f27ae59febab9 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:20:06 -0400 Subject: [PATCH 05/34] Delete sqrt.py --- Franklin/Math/sqrt.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Franklin/Math/sqrt.py diff --git a/Franklin/Math/sqrt.py b/Franklin/Math/sqrt.py deleted file mode 100644 index 5766991..0000000 --- a/Franklin/Math/sqrt.py +++ /dev/null @@ -1,22 +0,0 @@ -class Solution(object): - def mySqrt(self, x): - """ - :type x: int - :rtype: int - """ - #corner case - if (x==1): - return 1 - low = 0 - high = x - mid = (low+high)/2 - 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 From c054929ee7bd02c7efeb6720d4538cb7811a03cf Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:22:16 -0400 Subject: [PATCH 06/34] Create twoSum --- Franklin/twoSum | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Franklin/twoSum diff --git a/Franklin/twoSum b/Franklin/twoSum new file mode 100644 index 0000000..0564559 --- /dev/null +++ b/Franklin/twoSum @@ -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 From 28f3e84af5997947e2f77e32d1a31564e718381f Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:24:07 -0400 Subject: [PATCH 07/34] Create reverse.py --- Franklin/reverse.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Franklin/reverse.py diff --git a/Franklin/reverse.py b/Franklin/reverse.py new file mode 100644 index 0000000..827798b --- /dev/null +++ b/Franklin/reverse.py @@ -0,0 +1,22 @@ +#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 From afefc68af2e12d16b7b9046505c885be60656a86 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:24:30 -0400 Subject: [PATCH 08/34] Rename romanToInt to romanToInt.py --- Franklin/{romanToInt => romanToInt.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{romanToInt => romanToInt.py} (100%) diff --git a/Franklin/romanToInt b/Franklin/romanToInt.py similarity index 100% rename from Franklin/romanToInt rename to Franklin/romanToInt.py From 9edda9f08ef7acb527e9b710d28e49fb114ca3fb Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:24:47 -0400 Subject: [PATCH 09/34] Rename mySqrt to mySqrt.py --- Franklin/{mySqrt => mySqrt.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{mySqrt => mySqrt.py} (100%) diff --git a/Franklin/mySqrt b/Franklin/mySqrt.py similarity index 100% rename from Franklin/mySqrt rename to Franklin/mySqrt.py From 3daa46c93fae2ca107dfc20af5fbd14f34f9e848 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:24:59 -0400 Subject: [PATCH 10/34] Rename twoSum to twoSum.py --- Franklin/{twoSum => twoSum.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{twoSum => twoSum.py} (100%) diff --git a/Franklin/twoSum b/Franklin/twoSum.py similarity index 100% rename from Franklin/twoSum rename to Franklin/twoSum.py From 01fc7b312d5c52baef1217c06fe6c416e90504b3 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:26:34 -0400 Subject: [PATCH 11/34] Create isPalindrome.py --- Franklin/isPalindrome.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Franklin/isPalindrome.py diff --git a/Franklin/isPalindrome.py b/Franklin/isPalindrome.py new file mode 100644 index 0000000..50c1973 --- /dev/null +++ b/Franklin/isPalindrome.py @@ -0,0 +1,16 @@ +# 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 or ()): + return False + rev = 0 + while (x!=0): + rev = rev*10 + x%10 + x = x/10 + return (x==rev or x==rev/10) From 0190a8088173099f751e49f3672bd9809f7810ee Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 14:30:45 -0400 Subject: [PATCH 12/34] Create strStr.py --- Franklin/strStr.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Franklin/strStr.py diff --git a/Franklin/strStr.py b/Franklin/strStr.py new file mode 100644 index 0000000..9639d5c --- /dev/null +++ b/Franklin/strStr.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) From 525c27596515034b1aaf31626544619ec4ba1f33 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 15:43:16 -0400 Subject: [PATCH 13/34] Create isValid.py --- Franklin/isValid.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Franklin/isValid.py diff --git a/Franklin/isValid.py b/Franklin/isValid.py new file mode 100644 index 0000000..7fe4821 --- /dev/null +++ b/Franklin/isValid.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 == []) From 5ea260575b2e0f2df16d6617b321ee41a163cc2c Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 15:45:22 -0400 Subject: [PATCH 14/34] Rename isValid.py to validParenthesis.py --- Franklin/{isValid.py => validParenthesis.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{isValid.py => validParenthesis.py} (100%) diff --git a/Franklin/isValid.py b/Franklin/validParenthesis.py similarity index 100% rename from Franklin/isValid.py rename to Franklin/validParenthesis.py From 0dc4d2b37adf3c6949d7568a255ad2747c42819b Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 15:45:52 -0400 Subject: [PATCH 15/34] Rename reverse.py to reverseIntDigits.py --- Franklin/{reverse.py => reverseIntDigits.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{reverse.py => reverseIntDigits.py} (100%) diff --git a/Franklin/reverse.py b/Franklin/reverseIntDigits.py similarity index 100% rename from Franklin/reverse.py rename to Franklin/reverseIntDigits.py From 4e1ed4a7f560c0b54235e3d10dd32afa3b399a2f Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Mon, 5 Jun 2017 15:46:58 -0400 Subject: [PATCH 16/34] Rename strStr.py to 1stNeedle_haystack.py --- Franklin/{strStr.py => 1stNeedle_haystack.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{strStr.py => 1stNeedle_haystack.py} (100%) diff --git a/Franklin/strStr.py b/Franklin/1stNeedle_haystack.py similarity index 100% rename from Franklin/strStr.py rename to Franklin/1stNeedle_haystack.py From 0450639424deec29cbc40cd523dbd811758c763b Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:24:24 -0400 Subject: [PATCH 17/34] Rename 1stNeedle_haystack.py to 28.py --- Franklin/{1stNeedle_haystack.py => 28.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{1stNeedle_haystack.py => 28.py} (100%) diff --git a/Franklin/1stNeedle_haystack.py b/Franklin/28.py similarity index 100% rename from Franklin/1stNeedle_haystack.py rename to Franklin/28.py From a8b0c8807535dbb646fd8a5083c8cd3d0e0d0d80 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:24:36 -0400 Subject: [PATCH 18/34] Rename isPalindrome.py to 9.py --- Franklin/{isPalindrome.py => 9.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{isPalindrome.py => 9.py} (100%) diff --git a/Franklin/isPalindrome.py b/Franklin/9.py similarity index 100% rename from Franklin/isPalindrome.py rename to Franklin/9.py From c0cd229a65592bf68cfd989c07cd2ed793c0f38a Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:24:55 -0400 Subject: [PATCH 19/34] Rename mySqrt.py to 69.py --- Franklin/{mySqrt.py => 69.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{mySqrt.py => 69.py} (100%) diff --git a/Franklin/mySqrt.py b/Franklin/69.py similarity index 100% rename from Franklin/mySqrt.py rename to Franklin/69.py From fa08de0b663de4c6d4cb7d44619f06639cd8a5d3 Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:25:10 -0400 Subject: [PATCH 20/34] Rename reverseIntDigits.py to 7.py --- Franklin/{reverseIntDigits.py => 7.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{reverseIntDigits.py => 7.py} (100%) diff --git a/Franklin/reverseIntDigits.py b/Franklin/7.py similarity index 100% rename from Franklin/reverseIntDigits.py rename to Franklin/7.py From bf03bd2a67722c3cea91aee66074a9ea7f2e790d Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:25:26 -0400 Subject: [PATCH 21/34] Rename romanToInt.py to 13.py --- Franklin/{romanToInt.py => 13.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{romanToInt.py => 13.py} (100%) diff --git a/Franklin/romanToInt.py b/Franklin/13.py similarity index 100% rename from Franklin/romanToInt.py rename to Franklin/13.py From 32409db3c9ecd31639aa145042bad7ae2a6fd89a Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:25:43 -0400 Subject: [PATCH 22/34] Rename twoSum.py to 1.py --- Franklin/{twoSum.py => 1.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{twoSum.py => 1.py} (100%) diff --git a/Franklin/twoSum.py b/Franklin/1.py similarity index 100% rename from Franklin/twoSum.py rename to Franklin/1.py From d50e87174cbc410628cea04e047218aedda6338b Mon Sep 17 00:00:00 2001 From: Franklin Qin Date: Tue, 6 Jun 2017 14:26:05 -0400 Subject: [PATCH 23/34] Rename validParenthesis.py to 20.py --- Franklin/{validParenthesis.py => 20.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{validParenthesis.py => 20.py} (100%) diff --git a/Franklin/validParenthesis.py b/Franklin/20.py similarity index 100% rename from Franklin/validParenthesis.py rename to Franklin/20.py From e0f20d565391c7ad6f59afc019189cd260066739 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 22:29:12 -0400 Subject: [PATCH 24/34] Rename 1.py to twoSum.py --- Franklin/{1.py => twoSum.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{1.py => twoSum.py} (100%) diff --git a/Franklin/1.py b/Franklin/twoSum.py similarity index 100% rename from Franklin/1.py rename to Franklin/twoSum.py From 4330b7bc7bb4bea8be90d5f5684837dbc5f3de99 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 22:29:28 -0400 Subject: [PATCH 25/34] Rename 13.py to romanToInt.py --- Franklin/{13.py => romanToInt.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{13.py => romanToInt.py} (100%) diff --git a/Franklin/13.py b/Franklin/romanToInt.py similarity index 100% rename from Franklin/13.py rename to Franklin/romanToInt.py From a36967e327d54f703ef44d09d5cc056abc3cfa9d Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 22:32:36 -0400 Subject: [PATCH 26/34] Rename 20.py to validParenthesis.py --- Franklin/{20.py => validParenthesis.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{20.py => validParenthesis.py} (100%) diff --git a/Franklin/20.py b/Franklin/validParenthesis.py similarity index 100% rename from Franklin/20.py rename to Franklin/validParenthesis.py From 7b052ea8f9e61aa7b9d5a21ea2c88ce955121542 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 22:33:09 -0400 Subject: [PATCH 27/34] Rename 28.py to haystack.py --- Franklin/{28.py => haystack.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{28.py => haystack.py} (100%) diff --git a/Franklin/28.py b/Franklin/haystack.py similarity index 100% rename from Franklin/28.py rename to Franklin/haystack.py From 4d0dde914f76e4bf56d94822b63ee94577cf6980 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 23:06:00 -0400 Subject: [PATCH 28/34] Update and rename 69.py to mySqrt.py --- Franklin/{69.py => mySqrt.py} | 2 ++ 1 file changed, 2 insertions(+) rename Franklin/{69.py => mySqrt.py} (85%) diff --git a/Franklin/69.py b/Franklin/mySqrt.py similarity index 85% rename from Franklin/69.py rename to Franklin/mySqrt.py index e97e5af..649df78 100644 --- a/Franklin/69.py +++ b/Franklin/mySqrt.py @@ -22,3 +22,5 @@ def mySqrt(self, x): high = mid mid = (low+high)/2 return mid + + # This solution uses binary search, and I am thinking if Newton's method would work? From e0221a09ce40f996fc64106042064424d72fe2a2 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 23:07:47 -0400 Subject: [PATCH 29/34] Update and rename 7.py to reverseIntDigits.py --- Franklin/{7.py => reverseIntDigits.py} | 2 ++ 1 file changed, 2 insertions(+) rename Franklin/{7.py => reverseIntDigits.py} (90%) diff --git a/Franklin/7.py b/Franklin/reverseIntDigits.py similarity index 90% rename from Franklin/7.py rename to Franklin/reverseIntDigits.py index 827798b..92dda09 100644 --- a/Franklin/7.py +++ b/Franklin/reverseIntDigits.py @@ -20,3 +20,5 @@ def reverse(self, x): return 0 else: return x_reversed_int + + # This solution is not optimal. Try to think of another one. From ef678808caa7067d4e21ef3847d2f3316b27a1d5 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 23:08:05 -0400 Subject: [PATCH 30/34] Rename 9.py to isPalindrome.py --- Franklin/{9.py => isPalindrome.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Franklin/{9.py => isPalindrome.py} (100%) diff --git a/Franklin/9.py b/Franklin/isPalindrome.py similarity index 100% rename from Franklin/9.py rename to Franklin/isPalindrome.py From cad7ed988ff627cedc5d5bea72cf54fcfe9001e0 Mon Sep 17 00:00:00 2001 From: Ziyuan Qin Date: Tue, 20 Jun 2017 23:10:45 -0400 Subject: [PATCH 31/34] Update isPalindrome.py --- Franklin/isPalindrome.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Franklin/isPalindrome.py b/Franklin/isPalindrome.py index 50c1973..ebbd707 100644 --- a/Franklin/isPalindrome.py +++ b/Franklin/isPalindrome.py @@ -14,3 +14,4 @@ def isPalindrome(self, x): rev = rev*10 + x%10 x = x/10 return (x==rev or x==rev/10) +# what's wrong with the if condition? From ff4873bdb45c53ed3672a62d84b4c834870c85fa Mon Sep 17 00:00:00 2001 From: zq29 Date: Thu, 22 Jun 2017 00:09:06 -0400 Subject: [PATCH 32/34] restarting ... --- Franklin/addTwoNumbers.java | 69 +++++++++++++++++++++++++++++++++++++ Franklin/isPalindrome.java | 15 ++++++++ Franklin/isPalindrome.py | 14 ++++---- 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 Franklin/addTwoNumbers.java create mode 100644 Franklin/isPalindrome.java 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/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 index ebbd707..fbe4920 100644 --- a/Franklin/isPalindrome.py +++ b/Franklin/isPalindrome.py @@ -7,11 +7,11 @@ def isPalindrome(self, x): :type x: int :rtype: bool """ - if (x<0 or ()): + if (x<0): return False - rev = 0 - while (x!=0): - rev = rev*10 + x%10 - x = x/10 - return (x==rev or x==rev/10) -# what's wrong with the if condition? + + rev = 0; y = x + while (y!=0): + rev = rev*10 + y%10 + y = y/10 + return (x==rev) From 6287a01bfed588a96345f1a6efbec7678ff2ff8f Mon Sep 17 00:00:00 2001 From: zq29 Date: Sat, 24 Jun 2017 23:39:40 -0400 Subject: [PATCH 33/34] contest problem #1 --- Franklin/maximumProduct.java | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Franklin/maximumProduct.java 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; + } +} From a8c2f5756cab999978eb41adffbae6e6c0221438 Mon Sep 17 00:00:00 2001 From: zq29 Date: Sat, 24 Jun 2017 23:45:25 -0400 Subject: [PATCH 34/34] Updated README --- Franklin/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Franklin/README.md 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