mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -98,7 +98,8 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
# 方法 1: 数组在哈西法的应用
|
||||||
class Solution:
|
class Solution:
|
||||||
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
||||||
count = [0] * 2002
|
count = [0] * 2002
|
||||||
@ -113,6 +114,26 @@ class Solution:
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
```python
|
||||||
|
# 方法 2: map 在哈西法的应用
|
||||||
|
class Solution:
|
||||||
|
def uniqueOccurrences(self, arr: List[int]) -> bool:
|
||||||
|
ref = dict()
|
||||||
|
|
||||||
|
for i in range(len(arr)):
|
||||||
|
ref[arr[i]] = ref.get(arr[i], 0) + 1
|
||||||
|
|
||||||
|
value_list = sorted(ref.values())
|
||||||
|
|
||||||
|
for i in range(len(value_list) - 1):
|
||||||
|
if value_list[i + 1] == value_list[i]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
@ -59,6 +59,7 @@
|
|||||||
* 它的左、右子树也分别为二叉排序树
|
* 它的左、右子树也分别为二叉排序树
|
||||||
|
|
||||||
下面这两棵树都是搜索树
|
下面这两棵树都是搜索树
|
||||||
|
|
||||||
<img src='https://img-blog.csdnimg.cn/20200806190304693.png' width=600> </img></div>
|
<img src='https://img-blog.csdnimg.cn/20200806190304693.png' width=600> </img></div>
|
||||||
|
|
||||||
|
|
||||||
|
@ -803,6 +803,46 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
## 二分查找法
|
||||||
|
```python
|
||||||
|
def binarysearch(nums, target):
|
||||||
|
low = 0
|
||||||
|
high = len(nums) - 1
|
||||||
|
while (low <= high):
|
||||||
|
mid = (high + low)//2
|
||||||
|
|
||||||
|
if (nums[mid] < target):
|
||||||
|
low = mid + 1
|
||||||
|
|
||||||
|
if (nums[mid] > target):
|
||||||
|
high = mid - 1
|
||||||
|
|
||||||
|
if (nums[mid] == target):
|
||||||
|
return mid
|
||||||
|
|
||||||
|
return -1
|
||||||
|
```
|
||||||
|
|
||||||
|
## KMP
|
||||||
|
|
||||||
|
```python
|
||||||
|
def kmp(self, a, s):
|
||||||
|
# a: length of the array
|
||||||
|
# s: string
|
||||||
|
|
||||||
|
next = [0]*a
|
||||||
|
j = 0
|
||||||
|
next[0] = 0
|
||||||
|
|
||||||
|
for i in range(1, len(s)):
|
||||||
|
while j > 0 and s[j] != s[i]:
|
||||||
|
j = next[j - 1]
|
||||||
|
|
||||||
|
if s[j] == s[i]:
|
||||||
|
j += 1
|
||||||
|
next[i] = j
|
||||||
|
return next
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user