Skip to content

add moveZero 283 problem python code #55

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

Closed
Closed
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
16 changes: 16 additions & 0 deletions problems/136.single-number.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Your algorithm should have a linear runtime complexity. Could you implement it w

## 代码

Javascript:

```js
/*
* @lc app=leetcode id=136 lang=javascript
Expand Down Expand Up @@ -87,6 +89,20 @@ var singleNumber = function(nums) {
};
```

Python:

```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
no_duplicatearray = []
for x in nums:
if x not in no_duplicatearray:
no_duplicatearray.append(x)
else:
no_duplicatearray.remove(x)
return no_duplicatearray.pop()
```

## 延伸

有一个 n 个元素的数组,除了两个数只出现一次外,其余元素都出现两次,让你找出这两个只出现一次的数分别是几,要求时间复杂度为 O(n) 且再开辟的内存空间固定(与 n 无关)。
Expand Down
45 changes: 45 additions & 0 deletions problems/155.min-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ pop或者top的时候:
## 关键点

- 最小栈存储的不应该是真实值,而是真实值和min的差值
- 当栈为空的时候,return None
- top的时候涉及到对数据的还原,这里千万注意是**上一个**最小值

## 代码

Javascript:

```js
/*
* @lc app=leetcode id=155 lang=javascript
Expand Down Expand Up @@ -131,3 +134,45 @@ MinStack.prototype.getMin = function() {
*/
```

Python:

```python
class MinStack:

def __init__(self):
"""
initialize your data structure here.
"""
self.item = []
self.minStack = []
def push(self, x: int) -> None:
self.item.append(x)
if self.minStack:
x = min(x, self.minStack[-1])
self.minStack.append(x)

def pop(self) -> None:

if len(self.item) == 0:
return
else:
self.minStack.pop()
self.item.pop()

def top(self) -> int:
return self.item[len(self.item) - 1]

def getMin(self) -> int:
return self.minStack[-1]

# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()



```

57 changes: 57 additions & 0 deletions problems/26.remove-duplicates-from-sorted-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ for (int i = 0; i < len; i++) {

## 思路

Javascript:

使用快慢指针来记录遍历的坐标。

- 开始时这两个指针都指向第一个数字
Expand All @@ -55,18 +57,49 @@ for (int i = 0; i < len; i++) {

(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)

***

Python:

通过增加参考boolean数值来进行筛选,属于双指针的一种另类解法

- 创建一个result数组用来封装结果的数组。(下面的代码会将符合要求即没有重复的元素筛选出来) line 161

- 将第一个元素先append进去,这是为了防止range(0,0)的情况,并且第一个元素不可能重叠(因为他是第一个) line 162

- 创建双指针:

- 第一个指针:正常地从头走到尾部 line 163

- 第二个指针:从头开始扫,但是不会等于或大于第一个指针 line 165

- boolean默认为false。如果有重叠会变true,没有是false line 164, line 167, line 169

- 如果result是true,将元素加入result数组,否则跳过进入下一循环

## 关键点解析

Javascript:

- 双指针

这道题如果不要求,O(n)的时间复杂度, O(1)的空间复杂度的话,会很简单。
但是这道题是要求的,这种题的思路一般都是采用双指针

- 如果是数据是无序的,就不可以用这种方式了,从这里也可以看出排序在算法中的基础性和重要性。

***

Python:

- 数据复杂度 O(n^2)。不算很有效但是是一种很直觉上的brute force方法
- 对于没有排序的数组,可先考虑merge sort,然后再用算法解决重合问题

## 代码

* 语言支持: Javascript, Python

Javascript:
```js
/*
* @lc app=leetcode id=26 lang=javascript
Expand Down Expand Up @@ -145,3 +178,27 @@ var removeDuplicates = function(nums) {
return slowP + 1;
};
```
Python:




```python
class Solution:
def removeDuplicates(self,nums):
result = []
result.append(nums[0])
for n in range(len(nums)):
result_ref = False
for x in range(n):
if nums[x] == nums[n]:
result_ref = False
else:
result_ref = True
if result_ref:
result.append(nums[n])
else:
continue

return len(result)
```
22 changes: 22 additions & 0 deletions problems/283.move-zeroes.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ Minimize the total number of operations.

## 代码

- 语言支持:JS,Python


Javascript Code:
```js
/*
* @lc app=leetcode id=283 lang=javascript
Expand Down Expand Up @@ -80,3 +85,20 @@ var moveZeroes = function(nums) {
};
```


Python Code:

> 解题思想与上面JavaScript一致,做了少许代码优化(非性能优化,因为时间复杂度都是O(n)): 增加一个游标来记录下一个待处理的元素的位置,这样只需要写一次循环即可。


```python
def moveZeros(nums):
count = 0
for n in range(len(nums)):
el = nums[n]
if el != 0:
nums[n], nums[count] = nums[count], nums[n]
count += 1
return nums

```