Skip to content
Merged

pr #7

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions kaakxixi/contains_duplicate_2.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions kaakxixi/intersection_of_two_arrays.py
Original file line number Diff line number Diff line change
@@ -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)]
13 changes: 13 additions & 0 deletions kaakxixi/intersection_of_two_arrays_2.py
Original file line number Diff line number Diff line change
@@ -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?
"""
"""使用两个字典记录下两个数组中元素出现的次数"""
11 changes: 11 additions & 0 deletions kaakxixi/readme.md
Original file line number Diff line number Diff line change
@@ -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表中的元素的访问是常数时间级别.

44 changes: 44 additions & 0 deletions kaakxixi/sum_two_nums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
"""
"""方法1是使用list查询差值"""
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)]


"""方法2是使用dict查询差值"""
def twoSum2(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
#创建一个空字典
dic = {}
for i in range(len(nums)):
num = target - nums[i]
#字典d中存在nums[i]时
if nums[i] in dic:
return dic[nums[i]],i
#否则往字典增加键/值对
else:
dic[num] = i
#边往字典增加键/值对,边与nums[i]进行对比
18 changes: 18 additions & 0 deletions kaakxixi/valid_anagram.py
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions kaakxixi/word_pattern.py
Original file line number Diff line number Diff line change
@@ -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(' '))))