From ed24cc30a418546eefc00a3c231c2a8c60ee6452 Mon Sep 17 00:00:00 2001 From: kelthuzad_999 Date: Wed, 5 Dec 2018 21:37:59 +0800 Subject: [PATCH 1/4] first pr --- kaakxixi/contains_duplicate_2.py | 13 +++++++++++++ kaakxixi/intersection_of_two_arrays.py | 22 ++++++++++++++++++++++ kaakxixi/intersection_of_two_arrays_2.py | 13 +++++++++++++ kaakxixi/valid_anagram.py | 18 ++++++++++++++++++ kaakxixi/word_pattern.py | 24 ++++++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 kaakxixi/contains_duplicate_2.py create mode 100644 kaakxixi/intersection_of_two_arrays.py create mode 100644 kaakxixi/intersection_of_two_arrays_2.py create mode 100644 kaakxixi/valid_anagram.py create mode 100644 kaakxixi/word_pattern.py diff --git a/kaakxixi/contains_duplicate_2.py b/kaakxixi/contains_duplicate_2.py new file mode 100644 index 0000000..bb3ca37 --- /dev/null +++ b/kaakxixi/contains_duplicate_2.py @@ -0,0 +1,13 @@ +""" +Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. +""" +"""仍然用dict保存数组元素出现的位置,两种情况下更新""" + +class Solution(object): + def containsDuplicate2(self,nums,k): + dic = dict() + for index,value in enumerate(nums): + if value in dic and index - dic[value] <= k: + return True + dic[value] = index + return False \ No newline at end of file diff --git a/kaakxixi/intersection_of_two_arrays.py b/kaakxixi/intersection_of_two_arrays.py new file mode 100644 index 0000000..0040cfa --- /dev/null +++ b/kaakxixi/intersection_of_two_arrays.py @@ -0,0 +1,22 @@ +""" +Given two arrays, write a function to compute their intersection. +Example: +Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. +""" + +"""可以使用hash table保存下数组1的出现的元素,然后判断数组2中的各元素是否在数组1中出现过,但直接使用set更简单""" +class Solution(object): + def intersectionOfTwoArrays(self,nums1,nums2): + """ + :type nums1: List[int] + :type nums2: List[int] + :rtype: List[int] + """ + result = [] + for value in set(nums1): + if value in set(nums2): + result.append(value) + return result + + '''利用列表表达式''' + return [i for i in set(nums1) if i in set(nums2)] diff --git a/kaakxixi/intersection_of_two_arrays_2.py b/kaakxixi/intersection_of_two_arrays_2.py new file mode 100644 index 0000000..e767d01 --- /dev/null +++ b/kaakxixi/intersection_of_two_arrays_2.py @@ -0,0 +1,13 @@ +""" +Given two arrays, write a function to compute their intersection. +Example: +Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. +Note: +Each element in the result should appear as many times as it shows in both arrays. +The result can be in any order. +Follow up: +What if the given array is already sorted? How would you optimize your algorithm? +What if nums1's size is small compared to nums2's size? Which algorithm is better? +What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? +""" +"""使用两个字典记录下两个数组中元素出现的次数""" diff --git a/kaakxixi/valid_anagram.py b/kaakxixi/valid_anagram.py new file mode 100644 index 0000000..a83cb63 --- /dev/null +++ b/kaakxixi/valid_anagram.py @@ -0,0 +1,18 @@ +""" +Given two strings s and t, write a function to determine if t is an anagram of s. +For example, +s = "anagram", t = "nagaram", return true. +s = "rat", t = "car", return false. +Note: +You may assume the string contains only lowercase alphabets. +""" +"""用两个字典保存字符出现的情况,判断两个字典是否相同即可""" +class Solution(object): + def validAnagram(self,s,t): + dic1 = {} + dic2 = {} + for i in s: + dic1[i] = dic1.get(i,0) + 1 + for j in t: + dic2[j] = dic2.get(j,0) + 1 + return dic1 == dic2 \ No newline at end of file diff --git a/kaakxixi/word_pattern.py b/kaakxixi/word_pattern.py new file mode 100644 index 0000000..501bef7 --- /dev/null +++ b/kaakxixi/word_pattern.py @@ -0,0 +1,24 @@ +""" +Total Accepted: 76577 +Total Submissions: 233596 +Difficulty: Easy +Contributor: LeetCode +Given a pattern and a string str, find if str follows the same pattern. +Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. +Examples: +pattern = "abba", str = "dog cat cat dog" should return true. +pattern = "abba", str = "dog cat cat fish" should return false. +pattern = "aaaa", str = "dog cat cat dog" should return false. +pattern = "abba", str = "dog dog dog dog" should return false. +Notes: +You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space. +""" +class Solution(object): + def wordPattern(self,pattern,str): + """ + :type pattern: str + :type str: str + :rtype: bool + """ + return len(pattern) == len(str.split(' ')) and len(set(pattern)) == len( + set(str.split(' '))) == len(set(zip(pattern,str.split(' ')))) \ No newline at end of file From d6ebdd7a1882a5e4a49badb6b055c1a87effd141 Mon Sep 17 00:00:00 2001 From: kelthuzad_999 Date: Wed, 5 Dec 2018 22:33:53 +0800 Subject: [PATCH 2/4] first pr-kkx --- kaakxixi/sum_two_nums.md | 7 +++++++ kaakxixi/sum_two_nums.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 kaakxixi/sum_two_nums.md create mode 100644 kaakxixi/sum_two_nums.py diff --git a/kaakxixi/sum_two_nums.md b/kaakxixi/sum_two_nums.md new file mode 100644 index 0000000..bc5eae3 --- /dev/null +++ b/kaakxixi/sum_two_nums.md @@ -0,0 +1,7 @@ +算法分析: + +方法1. 整体时间复杂度为O(n^2). 由于python中的list对象实际上是链表,其查询效率为 O(n), 即这里的if 语句if num in nums复杂度为O(n), +再结合列表循环次数n, 整体时间复杂度为O(n^2). + +方法2. 整体时间复杂度为常数级别O(1). 使用散列hash表(python中的dict和set), 将差值target - nums[i]作为dict的键, 索引 i 作为dict的值, 判断列表中的值是否在dict中, 这里对hash表中的元素的访问是常数时间级别. + diff --git a/kaakxixi/sum_two_nums.py b/kaakxixi/sum_two_nums.py new file mode 100644 index 0000000..5aba98f --- /dev/null +++ b/kaakxixi/sum_two_nums.py @@ -0,0 +1,42 @@ +""" +给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 + +你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 + +示例: + +给定 nums = [2, 7, 11, 15], target = 9 + +因为 nums[0] + nums[1] = 2 + 7 = 9 +所以返回 [0, 1] +""" +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + for i in range(len(nums)): + num = target - nums[i] + if num in nums and nums.index(num) != i: + return [i,nums.index(num)] + + + def twoSum2(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + #创建一个空字典 + d = {} + for i in range(len(nums)): + num = target - nums[i] + #字典d中存在nums[i]时 + if nums[i] in d: + return d[nums[i]],i + #否则往字典增加键/值对 + else: + d[num] = i + #边往字典增加键/值对,边与nums[i]进行对比 \ No newline at end of file From 18b7453d2f5aca342afc189d3e60991205c2d7f1 Mon Sep 17 00:00:00 2001 From: kelthuzad_999 Date: Wed, 5 Dec 2018 22:47:48 +0800 Subject: [PATCH 3/4] first pr-kkx --- kaakxixi/sum_two_nums.md | 7 ------- kaakxixi/sum_two_nums.py | 10 ++++++---- 2 files changed, 6 insertions(+), 11 deletions(-) delete mode 100644 kaakxixi/sum_two_nums.md diff --git a/kaakxixi/sum_two_nums.md b/kaakxixi/sum_two_nums.md deleted file mode 100644 index bc5eae3..0000000 --- a/kaakxixi/sum_two_nums.md +++ /dev/null @@ -1,7 +0,0 @@ -算法分析: - -方法1. 整体时间复杂度为O(n^2). 由于python中的list对象实际上是链表,其查询效率为 O(n), 即这里的if 语句if num in nums复杂度为O(n), -再结合列表循环次数n, 整体时间复杂度为O(n^2). - -方法2. 整体时间复杂度为常数级别O(1). 使用散列hash表(python中的dict和set), 将差值target - nums[i]作为dict的键, 索引 i 作为dict的值, 判断列表中的值是否在dict中, 这里对hash表中的元素的访问是常数时间级别. - diff --git a/kaakxixi/sum_two_nums.py b/kaakxixi/sum_two_nums.py index 5aba98f..535d80a 100644 --- a/kaakxixi/sum_two_nums.py +++ b/kaakxixi/sum_two_nums.py @@ -10,6 +10,7 @@ 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] """ +"""方法1是使用list查询差值""" class Solution(object): def twoSum(self, nums, target): """ @@ -23,6 +24,7 @@ def twoSum(self, nums, target): return [i,nums.index(num)] +"""方法2是使用dict查询差值""" def twoSum2(self, nums, target): """ :type nums: List[int] @@ -30,13 +32,13 @@ def twoSum2(self, nums, target): :rtype: List[int] """ #创建一个空字典 - d = {} + dic = {} for i in range(len(nums)): num = target - nums[i] #字典d中存在nums[i]时 - if nums[i] in d: - return d[nums[i]],i + if nums[i] in dic: + return dic[nums[i]],i #否则往字典增加键/值对 else: - d[num] = i + dic[num] = i #边往字典增加键/值对,边与nums[i]进行对比 \ No newline at end of file From 3a95e25fed4c4fdf94eed83a3e9a4dc1b12cb6f2 Mon Sep 17 00:00:00 2001 From: kelthuzad_999 Date: Wed, 5 Dec 2018 22:51:39 +0800 Subject: [PATCH 4/4] first pr-kkx --- kaakxixi/readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 kaakxixi/readme.md diff --git a/kaakxixi/readme.md b/kaakxixi/readme.md new file mode 100644 index 0000000..1331f50 --- /dev/null +++ b/kaakxixi/readme.md @@ -0,0 +1,11 @@ +LeetCode 1 两数之和 +2018-12-05 +代码提交地址 https://leetcode-cn.com/problems/two-sum/submissions/ + +算法分析: + +方法1. 整体时间复杂度为O(n^2). 由于python中的list对象实际上是链表,其查询效率为 O(n), 即这里的if 语句if num in nums复杂度为O(n), +再结合列表循环次数n, 整体时间复杂度为O(n^2). + +方法2. 整体时间复杂度为常数级别O(1). 使用散列hash表(python中的dict和set), 将差值target - nums[i]作为dict的键, 索引 i 作为dict的值, 判断列表中的值是否在dict中, 这里对hash表中的元素的访问是常数时间级别. +