diff --git a/AlgorithmHomework1.md b/AlgorithmHomework1.md index 6f7048d..627bf24 100644 --- a/AlgorithmHomework1.md +++ b/AlgorithmHomework1.md @@ -7,11 +7,14 @@ 3道上课讲的LeetCode题目1,69,70 - ## Leetcode problem 1 (两数之和) - - 算法思路:
- 用字典记录数字,以便在找到成对的另一个数的时候,可以快速的检索到前一个数的索引 + - 算法思路: + + 用字典记录数字,以便在找到成对的另一个数的时候,可以快速的检索到前一个数的索引 + - Leetcode提交结果 ![result](AlgorithmHomework1.assets/7222676-c485f42b177afce2.png) + - 代码: ```python def twoSum(self, nums: List[int], target: int) -> List[int]: @@ -19,10 +22,10 @@ for index, num in enumerate(nums): if num in myDict: return [myDict[num], index] - else: + else: myDict[target - num] = index ``` - + - ## Leetcode problem 69 (x 的平方根) - 算法思路:二分法 diff --git a/AlgorithmHomework2.md b/AlgorithmHomework2.md index 10f9e52..f74c6d9 100644 --- a/AlgorithmHomework2.md +++ b/AlgorithmHomework2.md @@ -10,7 +10,9 @@ ## 题解 -- ### Leetcode problem 7([整数反转](https://leetcode-cn.com/problems/reverse-integer/)) +- ### Leetcode problem 7 + + > [整数反转](https://leetcode-cn.com/problems/reverse-integer/) - **算法思路:** @@ -37,7 +39,9 @@ return 0 if result > INT_MAX_VAL or result < INT_MIN_VAL else isNegitive * result ``` -- ### Leetcode problem 13([罗马数字转整数](https://leetcode-cn.com/problems/roman-to-integer/)) +- ### Leetcode problem 13 + + > [罗马数字转整数](https://leetcode-cn.com/problems/roman-to-integer/) - **算法思路:** @@ -81,11 +85,13 @@ return result ``` -- ### Leetcode problem 3([加一](https://leetcode-cn.com/problems/plus-one/)) +- ### Leetcode problem 3 + + > [加一](https://leetcode-cn.com/problems/plus-one/) - **算法思路:** - - - 从后往前遍历,每次 + 1,如果和等于10,则发生进位,否则算法返回 + + - 从后往前遍历,每次 + 1,如果和等于10,则发生进位,否则算法返回 - 遍历结束发现 *tmp* 仍然不为0,则表示数组的值为全9,需要在数组前添加一个元素 - **Leetcode提交结果:** @@ -93,7 +99,7 @@ ![image-20200320134010709](AlgorithmHomework2.assets/image-20200320134010709.png) - **代码:** - + ```python class Solution: def plusOne(self, digits: List[int]) -> List[int]: @@ -105,7 +111,7 @@ index -= 1 if tmp != 0: digits.insert(0, 1) - return digits + return digits ``` - + \ No newline at end of file diff --git a/AlgorithmHomework3.md b/AlgorithmHomework3.md index e74a9d2..968fcf1 100644 --- a/AlgorithmHomework3.md +++ b/AlgorithmHomework3.md @@ -10,7 +10,9 @@ Leetcode: 35,58,83 ## 题解: -- ### Leetcode problem 35([搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/)) +- ### Leetcode problem 35 + + > [搜索插入位置](https://leetcode-cn.com/problems/search-insert-position/) - **算法思路:** @@ -43,7 +45,9 @@ Leetcode: 35,58,83 return right + 1 ``` -- ### Leetcode problem 58([最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word/)) +- ### Leetcode problem 58 + + > [最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word/) - **算法思路:** @@ -78,11 +82,13 @@ Leetcode: 35,58,83 return count ``` -- ### Leetcode problem 83([删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)) +- ### Leetcode problem 83 + + > [删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/) - **算法思路:** - - - 用一个set记录当前遍历过的值 + + - 用一个set记录当前遍历过的值 - 遍历到每一个节点,用set检查当前值是否存在于set,如果存在则删除该节点 - **Leetcode提交结果:** @@ -90,7 +96,7 @@ Leetcode: 35,58,83 ![image-20200313213736914](https://raw.githubusercontent.com/SunnyQjm/picgo_0/master/img/image-20200313211905775.png) - **代码:** - + ```python class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: @@ -105,7 +111,7 @@ Leetcode: 35,58,83 else: s.add(cur.val) pre, cur = cur, cur.next - return head + return head ``` - + \ No newline at end of file diff --git a/AlgorithmHomework4.assets/image-20200326200557425.png b/AlgorithmHomework4.assets/image-20200326200557425.png new file mode 100644 index 0000000..d92794c Binary files /dev/null and b/AlgorithmHomework4.assets/image-20200326200557425.png differ diff --git a/AlgorithmHomework4.assets/image-20200326210047592.png b/AlgorithmHomework4.assets/image-20200326210047592.png new file mode 100644 index 0000000..8b052f4 Binary files /dev/null and b/AlgorithmHomework4.assets/image-20200326210047592.png differ diff --git a/AlgorithmHomework4.assets/image-20200326211143566.png b/AlgorithmHomework4.assets/image-20200326211143566.png new file mode 100644 index 0000000..699ded5 Binary files /dev/null and b/AlgorithmHomework4.assets/image-20200326211143566.png differ diff --git a/AlgorithmHomework4.md b/AlgorithmHomework4.md new file mode 100644 index 0000000..ea23d7c --- /dev/null +++ b/AlgorithmHomework4.md @@ -0,0 +1,124 @@ +# Algorithm Homework 4 + +> 姓名:阙建明 +> +> 学号:1901213139 + +## 作业要求: + +Leetcode: 16,17,19 + +## 题解: + +- ### Leetcode problem 16 + + > [最接近的三数之和](https://leetcode-cn.com/problems/3sum-closest/) + + - **算法思路:** + + - 首先对nums进行从小到达排序; + - 然后依次遍历0 ~ size - 2,固定第一个数i,对i + 1 ~ size - 1的数用双指针法处理; + - 通过忽略已处理过的数来实现第一个数选取时的剪枝操作 + + - **Leetcode提交结果:** + + ![image-20200326200557425](AlgorithmHomework4.assets/image-20200326200557425.png) + + - **代码:** + + ```python + class Solution: + def threeSumClosest(self, nums: List[int], target: int) -> int: + if len(nums) < 3: + return 0 + elif len(nums) == 3: + return nums[0] + nums[1] + nums[2] + nums = sorted(nums) # 先排序 + result, lastFirst = nums[0] + nums[1] + nums[2], nums[0] - 1 + for i in range(0, len(nums) - 2): + if nums[i] == lastFirst: + continue + left, right, lastFirst = i + 1, len(nums) - 1, nums[i] + while left < right: + tmp = nums[i] + nums[left] + nums[right] + if abs(target - tmp) < abs(target - result): + result = tmp + if tmp < target: + left += 1 + elif tmp > target: + right -= 1 + else: + return result + return result + ``` + +- ### Leetcode problem 17 + + > [电话号码的字母组合](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/) + + - **算法思路:** + + - 采用回溯法递归遍历 + + - **Leetcode提交结果:** + + ![image-20200326210047592](AlgorithmHomework4.assets/image-20200326210047592.png) + + - **代码:** + + ```python + class Solution: + numMap = {'2': "abc", '3': "def", '4': "ghi", '5': "jkl", '6': "mno", '7': "pqrs", '8': "tuv", '9': "wxyz"} + result = [] + def _letterCombinations(self, path, digits: str, index): + if index == len(digits) - 1: + for ch in self.numMap[digits[index]]: + self.result.append(path + ch) + return + for ch in self.numMap[digits[index]]: + path = path + ch + self._letterCombinations(path, digits, index + 1) + path = path[:len(path) - 1] + + def letterCombinations(self, digits: str) -> List[str]: + if digits == "": + return [] + self.result = [] + self._letterCombinations("", digits, 0) + return self.result + ``` + +- ### Leetcode problem 19 + + > [删除链表的倒数第N个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/) + + - **算法思路:** + + - 用快慢指针法 + - 先让快指针走n步,然后让快慢指针同时右移,快指针为None时,慢指针指向的节点即为要删除的节点; + - 对要删除的节点是头结点的情况进行特判。 + +- **Leetcode提交结果:** + + ![image-20200326211143566](AlgorithmHomework4.assets/image-20200326211143566.png) + + - **代码:** + + ```python + class Solution: + def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: + fast, slow, pre = head, head, self + try: + for i in range(n): + fast = fast.next + except BaseException: + return head + if fast is None: + return head.next + while fast is not None: + fast, slow, pre = fast.next, slow.next, slow + pre.next = slow.next + return head + ``` + + \ No newline at end of file diff --git a/README.md b/README.md index 74914b1..6628308 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,5 @@ - [第一次作业](AlgorithmHomework1.md) - [第二次作业](AlgorithmHomework2.md) -- [第三次作业](AlgorithmHomework3.md) \ No newline at end of file +- [第三次作业](AlgorithmHomework3.md) +- [第四次作业](AlgorithmHomework4.md) \ No newline at end of file