From a4c23676efeecb44f9362f42e6886c3fb036babf Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 15:25:29 +0800 Subject: [PATCH 1/6] Update solution 062 [Python3] --- solution/062.Unique Paths/Solution.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solution/062.Unique Paths/Solution.py diff --git a/solution/062.Unique Paths/Solution.py b/solution/062.Unique Paths/Solution.py new file mode 100644 index 0000000000000..a0805d3d68a9c --- /dev/null +++ b/solution/062.Unique Paths/Solution.py @@ -0,0 +1,15 @@ +class Solution: + def uniquePaths(self, m, n): + """ + :type m: int + :type n: int + :rtype: int + """ + res = [[0]*m]*n + for i in range(n): + for j in range(m): + if i == 0 or j==0: + res[i][j] = 1 + else: + res[i][j] = res[i][j-1]+res[i-1][j] + return res[n-1][m-1] \ No newline at end of file From cb614bc015f5d68c55fbbf5654d52eafa736a3cd Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 15:25:41 +0800 Subject: [PATCH 2/6] Update solution 063 [Python3] --- solution/063.Unique Paths II/Solution.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/063.Unique Paths II/Solution.py diff --git a/solution/063.Unique Paths II/Solution.py b/solution/063.Unique Paths II/Solution.py new file mode 100644 index 0000000000000..1a007d3f53c2a --- /dev/null +++ b/solution/063.Unique Paths II/Solution.py @@ -0,0 +1,22 @@ +class Solution(object): + def uniquePathsWithObstacles(self, obstacleGrid): + """ + :type obstacleGrid: List[List[int]] + :rtype: int + """ + m, n = len(obstacleGrid), len(obstacleGrid[0]) + martix = [[0] * n] * m + for i in range(m): + for j in range(n): + if obstacleGrid[i][j] == 1: + martix[i][j] = 0 + else: + if i == 0 and j == 0: + martix[i][j] = 1 + elif i == 0: + martix[i][j] = martix[i][j - 1] + elif j == 0: + martix[i][j] = martix[i - 1][j] + else: + martix[i][j] = martix[i - 1][j] + martix[i][j - 1] + return martix[m - 1][n - 1] \ No newline at end of file From 37432b7f9bc3dd2cd7d8536f512178d883167185 Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 15:25:55 +0800 Subject: [PATCH 3/6] Update solution 070 [Python3] --- solution/070.Climbing Stairs/Solution.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/070.Climbing Stairs/Solution.py diff --git a/solution/070.Climbing Stairs/Solution.py b/solution/070.Climbing Stairs/Solution.py new file mode 100644 index 0000000000000..6175378fff60d --- /dev/null +++ b/solution/070.Climbing Stairs/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def climbStairs(self, n): + """ + :type n: int + :rtype: int + """ + if n<3: + return n + res=[None]*(n+1) + res[1]=1 + res[2]=2 + for i in range(3,n+1): + res[i]=res[i-1]+res[i-2] + return res[n] \ No newline at end of file From 18054c8169c849a3eff22329fcdc071cb62c0277 Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 16:33:33 +0800 Subject: [PATCH 4/6] Update solution 053 [Python3] --- solution/053.Maximum Subarray/Solution.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 solution/053.Maximum Subarray/Solution.py diff --git a/solution/053.Maximum Subarray/Solution.py b/solution/053.Maximum Subarray/Solution.py new file mode 100644 index 0000000000000..603a808519ead --- /dev/null +++ b/solution/053.Maximum Subarray/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def maxSubArray(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + n=len(nums) + if n == 1: + return nums[0] + res=[0]*n + res[0]=nums[0] + max0=nums[0] + for i in range(1,n): + res[i]=max(res[i-1]+nums[i],nums[i]) + max0=max(res[i],max0) + return max0 \ No newline at end of file From 785e63ba99dc4c5445969da19911e44dd48a5549 Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 16:34:03 +0800 Subject: [PATCH 5/6] Update solution 153 [Python3] --- .../Solution.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 solution/153.Find Minimum in Rotated Sorted Array/Solution.py diff --git a/solution/153.Find Minimum in Rotated Sorted Array/Solution.py b/solution/153.Find Minimum in Rotated Sorted Array/Solution.py new file mode 100644 index 0000000000000..e1633d97956da --- /dev/null +++ b/solution/153.Find Minimum in Rotated Sorted Array/Solution.py @@ -0,0 +1,30 @@ +class Solution: + def findMin(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return min(nums) + +class Solution: + def findMin(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + length=len(nums) + if length == 1: + return nums[0] + l=0 + r=length-1 + m=r//2 + + while lnums[m]: + r=m + else: + l=m+1 + m=(l+r)//2 + return nums[l] \ No newline at end of file From 689cc692cca78d2038fa9e52a4620feaefa0b9cd Mon Sep 17 00:00:00 2001 From: chakyam Date: Sun, 23 Sep 2018 16:34:29 +0800 Subject: [PATCH 6/6] Update solution 198 [Python3] --- solution/198.House Robber/Solution.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/198.House Robber/Solution.py diff --git a/solution/198.House Robber/Solution.py b/solution/198.House Robber/Solution.py new file mode 100644 index 0000000000000..52c288c3ec6ee --- /dev/null +++ b/solution/198.House Robber/Solution.py @@ -0,0 +1,19 @@ +class Solution: + def rob(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + length=len(nums) + if length == 0: + return 0 + if length == 1: + return nums[0] + res=[0]*length + res[0]=nums[0] + res[1]=max(nums[0],nums[1]) + + for i in range(2,length): + res[i]=max(nums[i]+res[i-2],res[i-1]) + + return res[-1] \ No newline at end of file