diff --git a/chapter4/3_remove-duplicates-from-sorted-list.py b/chapter4/3_remove-duplicates-from-sorted-list.py new file mode 100644 index 0000000..f318007 --- /dev/null +++ b/chapter4/3_remove-duplicates-from-sorted-list.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# coding=utf-8 + +################################################################################ +# Leetcode 83 删除排序链表中的重复元素 +# +# 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 +# +# 示例 1: +# 输入: 1->1->2 +# 输出: 1->2 +# +# 示例 2: +# 输入: 1->1->2->3->3 +# 输出: 1->2->3 +################################################################################ + + +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + + def __repr__(self): + if self: + return "{}->{}".format(self.val, repr(self.next)) + + +class Solution: + def deleteDuplicates(self, head): + """ + :type head: ListNode + :rtypr ListNode + + (Knowledge) + + 1. 用两个指针,pre指向当前遍历节点的前一个节点,cur指向当前节点; + 2. 用last记录上一次访问的数字; + 3. 每次查看当前值和上一次访问的数字是否相同,相同则执行删除节点操作,不相同则更新last和两个指针 + """ + if not head: + return None + pre, last, cur = head, head.val, head.next + + while cur: + if cur.val == last: + pre.next = cur.next + else: + last, pre = cur.val, cur + cur = cur.next + + return head + + +if __name__ == '__main__': + solution = Solution() + h1 = ListNode(1) + h1.next = ListNode(1) + h1.next.next = ListNode(2) + print(solution.deleteDuplicates(h1), "= 1->2") + + h1 = ListNode(1) + h1.next = ListNode(1) + h1.next.next = ListNode(2) + h1.next.next.next = ListNode(3) + h1.next.next.next.next = ListNode(3) + print(solution.deleteDuplicates(h1), "= 1->2->3") diff --git a/chapter4/4_single-number.py b/chapter4/4_single-number.py new file mode 100644 index 0000000..61e943c --- /dev/null +++ b/chapter4/4_single-number.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# coding=utf-8 + +###################################################################################### +# Leetcode 136 只出现一次的数字 +# +# 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 +# +# 说明: +# 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? +# +# 示例 1: +# 输入: [2,2,1] +# 输出: 1 +# +# 示例 2: +# 输入: [4,1,2,1,2] +# 输出: 4 +# +# PS: 本题考察异或(^)的理解和使用 +###################################################################################### + +class Solution: + def singleNumber(self, nums): + """ + :type: nums: List[int] + :rtype: int + + (Knowledge) + + 思路: + 1. 异或(^)操作 => 是一种位操作,两个数字进行异或,对应位相同则为0,不同则为1(即相同的数字异或得0,0和任意数字异或都得原数字) + 2. 由于题中nums数组除了目标值以外,所有的数字都出现了两次,所以把数组中所有的数字都进行异或之后,得到的结果即为目标值 + """ + for i in range(1, len(nums)): + nums[0] = nums[0] ^ nums[i] + return nums[0] + + +if __name__ == '__main__': + solution = Solution() + print(solution.singleNumber([2, 2, 1]), "= 1") + print(solution.singleNumber([4, 1, 2, 1, 2]), "= 4") diff --git a/chapter4/5_implement-strStr.py b/chapter4/5_implement-strStr.py new file mode 100644 index 0000000..03db277 --- /dev/null +++ b/chapter4/5_implement-strStr.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# coding=utf-8 + +#################################################################################### +# Leetcode 28 实现 strStr() +# +# 实现 strStr() 函数。 +# 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。 +# +# 示例 1: +# 输入: haystack = "hello", needle = "ll" +# 输出: 2 +# +# 示例 2: +# 输入: haystack = "aaaaa", needle = "bba" +# 输出: -1 +# +# 说明: +# 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。 +# 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。 +#################################################################################### + +class Solution: + def strStr(self, haystack, needle): + """ + :type haystack: str + :type needle: str + :rtype int + + (knowledge) + + 思路: + 1. 对于haystack中每个可能的字符,判断以它为起始的子串是否和needle相等; + """ + if not needle: + return 0 + for i in range(0, len(haystack) - len(needle) + 1): + if haystack[i : i + len(needle)] == needle: + return i + return -1 + + +if __name__ == '__main__': + solution = Solution() + print(solution.strStr("hello", "ll"), "= 2") + print(solution.strStr("aaaaa", "bba"), "= -1") + print(solution.strStr("aaaaa", ""), "= 0")