Skip to content

Commit

Permalink
[ADD] 添加containsDuplicate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangsir404 committed Apr 3, 2020
1 parent 5fcb041 commit b060556
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
21 changes: 21 additions & 0 deletions hashmap/java/containsDupliate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.*;

/**
* @author rivir
* @date 2020/3/25
* 217. 存在重复元素 https://leetcode-cn.com/problems/contains-duplicate/
*/

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> m = new HashSet<>();
for(Integer i: nums){
if(m.contains(i)){
return true;
}else{
m.add(i);
}
}
return false;
}
}
35 changes: 29 additions & 6 deletions hashmap/python/136_singleNumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@
class Solution(object):
def singleNumber(self, nums):
"""
思路: 使用异或 时间复杂度O(n) 空间复杂度O(1)
0 ^ i = i
i ^ i = 0
0 ^ j ^ i ^ i = j
思路1: 使用哈希表,时间复杂度O(n), 空间复杂度O(n)
:type nums: List[int]
:rtype: int
"""
nums_map = {}
for i in nums:
if i in nums_map:
nums_map[i] += 1
else:
nums_map[i] = 1
for key, value in nums_map.items():
if value == 1:
return key

def singleNumber2(self, nums):
"""
思路2: 使用异或, 时间复杂度O(n), 空间复杂度O(1)
因为题目描述是某个元素出现一次,其他元素都出现两次,那么我们就可以让0和nums每个元素都异或一次,就会得到
那个只出现一次的元素,因为相同元素的值异或为0.
0 ^ i = i
i ^ i = 0
0 ^ j ^ i ^ i = j
"""
i = 0
for j in nums:
i = i ^ j
return i
return i


if __name__ == '__main__':
s = Solution()
nums = [2,2,1]
print s.singleNumber2(nums)
23 changes: 23 additions & 0 deletions hashmap/python/containsDuplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python
#coding:utf-8

"""
217. 存在重复元素
https://leetcode-cn.com/problems/contains-duplicate/
"""

class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
num_dic = {}
for i in nums:
if i in num_dic:
return True
else:
num_dic[i] = 1;

return False
11 changes: 0 additions & 11 deletions java/java.iml

This file was deleted.

14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ https://leetcode-cn.com/
- [ ] Reverse Words in a String(翻转字符串里的单词)

### sort
- [x] 堆排序算法 heap_sort.py
- [x] 冒泡排序 bubble_sort.py
- [x] 插入排序 insert_sort.py
- [x] 选择排序 select_sort.py
- [x] 堆排序算法 heap_sort.py
- [x] 冒泡排序 bubble_sort.py bubble_sort.java
- [x] 插入排序 insert_sort.py insert_sort.java
- [x] 选择排序 select_sort.py select_sort.java

### binarysearch
- [x] 搜索插入位置 searchInsert.py
Expand All @@ -55,13 +55,13 @@ https://leetcode-cn.com/
- [ ] Design Circular Deque(设计一个双端队列)https://leetcode-cn.com/problems/design-circular-deque/

## queue
- [x] Queue模块的类的实现原理 queue
- [x] Queue模块的类的实现原理 queue.py

## heap
- [x] 面试题40. 最小的k个数 getLeastNumbers
- [x] 面试题40. 最小的k个数 getLeastNumbers.py

## hashmap
- [x] 136. 只出现一次的数字 singleNumber
- [x] 136. 只出现一次的数字 singleNumber.py singleNumber.java


## bitmap
Expand Down

0 comments on commit b060556

Please sign in to comment.