Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13 个链表算法,使用python3.7.4编写 #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ public static int Fibonacci( int n ){
}
```

### Python

```
def fibonacci(num):
if num == 0 or num == 1:
return 1
return fibonacci(num-1) + fibonacci(num-2)
```
17 changes: 17 additions & 0 deletions [1]. Math Implementation/1.2 Binary Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,20 @@ public static int binary_search( int[] array, int target){

}
```

### Python

```
def binary_search(nums, low, high, key):
if not nums or low > high:
return -1
if key < nums[low] or key > nums[high]:
return -1
mid = (low + high)//2
if nums[mid] == key:
return mid
elif nums[mid] > key:
return binary_search(nums, low, mid-1, key)
elif nums[mid] < key:
return binary_search(nums, mid+1, high, key)
```
11 changes: 11 additions & 0 deletions [1]. Math Implementation/1.3 Is Prime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public static boolean is_prime_1 ( int num){
```


### Python

```
def isPrime(num):
if num <= 0:
return False
for i in range(2,num):
if num % i == 0:
return False
return True
```



22 changes: 22 additions & 0 deletions [1]. Math Implementation/1.4 Is Ugly Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,25 @@ class Solution {
}
}
```



### Python

```
def isUgly(num):
if num < 1:
return False
if num == 1:
return True
res = num
while res%2 == 0:
res = res//2
while res%3 == 0:
res = res//3
while res%5 == 0:
res = res//5
if res == 1:
return True
return False
```
12 changes: 11 additions & 1 deletion [1]. Math Implementation/1.5 Is Power Of Two/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,15 @@ class Solution {
}
```

### Python


```
def isPowerOfTwo(num):
if num <= 0:
return False
while num%2 == 0:
num = num//2
if num == 1:
return True
return False
```
10 changes: 10 additions & 0 deletions [1]. Math Implementation/1.6 Is Power Of Three/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ public boolean isPowerOfThree(int n) {



### Python

```
def isPowerOfThree(num):
if num == 1 or num == 0:
return True
if num >= 3 and num % 3 == 0:
return isPowerOfThree(num//3)
```

20 changes: 20 additions & 0 deletions [1]. Math Implementation/1.7 Count Primes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ boolean isPrime(int n){

```

### Python

```
def isPrime(num):
if num <= 0:
return False
for i in range(2,num):
if num % i == 0:
return False
return True

def get_prime_count(n):
count = 0
if n > 2:
count += 1
for i in range(3,n):
if isPrime(i):
count += 1
return count
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public static void BubbleSort(int[] arr) {
}
```

### Python

```
def bubble_sort(nums):
if len(nums) == 0:
return
count = len(nums)
for i in range(0, count):
for j in range(count-1, i, -1):
last = nums[count-j]
last_two = nums[count-j-1]
if last < last_two:
temp = nums[count-j]
nums[count-j] = last_two
nums[count-j-1] = temp
```
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ public static void SelectionSort(int[] arr) {
}
}
```

### Python

```
def select_sort(nums):
if len(nums) == 0:
return
count = len(nums)
for i in range(0, count):
for j in range(i+1, count):
if nums[i] > nums[j]:
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ public static void InsertionSort(int[] arr) {
}
}
```

### Python

```
def insert_sort_2(nums):
count = len(nums)
if count == 0:
return
for i in range(1,count):
for j in range(i,0, -1):
if nums[j] < nums[j-1]:
temp = nums[j-1]
nums[j-1] = nums[j]
nums[j] = temp
```
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,26 @@ public static void swap(int a,int b){
}
```


### Python

```
def quick_sort(nums, left, right):
if left < right:
index = get_index(nums, left, right)
quick_sort(nums, left, index - 1)
quick_sort(nums, index + 1, right)
def get_index(nums, left, right):
base = nums[left]
i, j = left, right
while i < j :
'''直到不满足'''
while i < j and nums[j] > base:
j -= 1
nums[i] = nums[j]
while i < j and nums[i] < base:
i += 1
nums[j] = nums[i]
nums[i] = base
return i
```
29 changes: 29 additions & 0 deletions [3]. Linked List/3.1 Linked List Cycle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,32 @@ public class Solution {
}
}
```

### Python3.7

```

'''
class ListNode:
def __init__(self, value):
self.value = value
self.next = None
'''

def is_circle(node):
if not node:
return False
l1 = node
l2 = node
res = False
while l1.next != None and l2.next != None:
l1 = l1.next
if l2.next.next:
l2 = l2.next.next
if l1 == l2:
# print(l1.value, l2.value, sep="**")
res = True
break
return res

```
30 changes: 30 additions & 0 deletions [3]. Linked List/3.10 Remove Duplicates II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,34 @@ class Solution {

}
}
```

### Python3.7

```

'''
class ListNode:
def __init__(self, value):
self.value = value
self.next = None
'''

def del_dup_node_2(node):
if not node:
return
fake_head = ListNode(0)
fake_head.next = node
pre = fake_head
cur = node
while cur.next:
while cur.next.value == cur.value:
cur = cur.next
if pre.next == cur:
pre = pre.next
else:
pre.next = cur.next
cur = cur.next
return fake_head.next

```
26 changes: 26 additions & 0 deletions [3]. Linked List/3.11 Remove Linked List Elements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,30 @@ class Solution {
return returnNode;
}
}
```

### Python3.7

```

'''
class ListNode:
def __init__(self, value):
self.value = value
self.next = None
'''

def del_node_value(node, value):
if not node:
return None
fake_head = ListNode(-1)
fake_head.next = node
cur = fake_head
while cur.next:
if cur.next.value == value:
cur.next = cur.next.next
else:
cur = cur.next
return fake_head.next

```
39 changes: 39 additions & 0 deletions [3]. Linked List/3.12 Partition Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,43 @@ class Solution {

}
}
```

### Python3.7

```

'''
class ListNode:
def __init__(self, value):
self.value = value
self.next = None
'''

def partition_node_value(node, value):
if not node:
return
l, l_pre = None, None
h, h_pre = None, None
cur = node
while cur:
if cur.value > value:
if not h:
h = cur
h_pre = cur
else:
h_pre.next = cur
h_pre = cur
else:
if not l:
l = cur
l_pre = cur
else:
l_pre.next = cur
l_pre = cur
cur = cur.next
h_pre.next = None
l_pre.next = h
return l

```
Loading