mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'master' into master
This commit is contained in:
@ -221,6 +221,40 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func threeSum(nums []int)[][]int{
|
||||||
|
sort.Ints(nums)
|
||||||
|
res:=[][]int{}
|
||||||
|
|
||||||
|
for i:=0;i<len(nums)-2;i++{
|
||||||
|
n1:=nums[i]
|
||||||
|
if n1>0{
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if i>0&&n1==nums[i-1]{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l,r:=i+1,len(nums)-1
|
||||||
|
for l<r{
|
||||||
|
n2,n3:=nums[l],nums[r]
|
||||||
|
if n1+n2+n3==0{
|
||||||
|
res=append(res,[]int{n1,n2,n3})
|
||||||
|
for l<r&&nums[l]==n2{
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
for l<r&&nums[r]==n3{
|
||||||
|
r--
|
||||||
|
}
|
||||||
|
}else if n1+n2+n3<0{
|
||||||
|
l++
|
||||||
|
}else {
|
||||||
|
r--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,7 +112,28 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Go:
|
||||||
|
```Go
|
||||||
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||||
|
result:=&ListNode{}
|
||||||
|
result.Next=head
|
||||||
|
var pre *ListNode
|
||||||
|
cur:=result
|
||||||
|
|
||||||
|
i:=1
|
||||||
|
for head!=nil{
|
||||||
|
if i>=n{
|
||||||
|
pre=cur
|
||||||
|
cur=cur.Next
|
||||||
|
}
|
||||||
|
head=head.Next
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
pre.Next=pre.Next.Next
|
||||||
|
return result.Next
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -181,7 +181,26 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func isValid(s string) bool {
|
||||||
|
hash := map[byte]byte{')':'(', ']':'[', '}':'{'}
|
||||||
|
stack := make([]byte, 0)
|
||||||
|
if s == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
if s[i] == '(' || s[i] == '[' || s[i] == '{' {
|
||||||
|
stack = append(stack, s[i])
|
||||||
|
} else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] {
|
||||||
|
stack = stack[:len(stack)-1]
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return len(stack) == 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,9 +124,19 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
|
i,n = 0,len(nums)
|
||||||
|
for j in range(n):
|
||||||
|
if nums[j] != val:
|
||||||
|
nums[i] = nums[j]
|
||||||
|
i += 1
|
||||||
|
return i
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```go
|
```go
|
||||||
@ -162,4 +172,4 @@ var removeElement = (nums, val) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -237,6 +237,22 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def searchInsert(self, nums: List[int], target: int) -> int:
|
||||||
|
left, right = 0, len(nums) - 1
|
||||||
|
|
||||||
|
while left <= right:
|
||||||
|
middle = (left + right) // 2
|
||||||
|
|
||||||
|
if nums[middle] < target:
|
||||||
|
left = middle + 1
|
||||||
|
elif nums[middle] > target:
|
||||||
|
right = middle - 1
|
||||||
|
else:
|
||||||
|
return middle
|
||||||
|
return right + 1
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
@ -175,8 +175,29 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func maxSubArray(nums []int) int {
|
||||||
|
if len(nums)<1{
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
dp:=make([]int,len(nums))
|
||||||
|
result:=nums[0]
|
||||||
|
dp[0]=nums[0]
|
||||||
|
for i:=1;i<len(nums);i++{
|
||||||
|
dp[i]=max(dp[i-1]+nums[i],nums[i])
|
||||||
|
result=max(dp[i],result)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a,b int)int{
|
||||||
|
if a>b{
|
||||||
|
return a
|
||||||
|
}else{
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -230,7 +230,20 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func climbStairs(n int) int {
|
||||||
|
if n==1{
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
dp:=make([]int,n+1)
|
||||||
|
dp[1]=1
|
||||||
|
dp[2]=2
|
||||||
|
for i:=3;i<=n;i++{
|
||||||
|
dp[i]=dp[i-1]+dp[i-2]
|
||||||
|
}
|
||||||
|
return dp[n]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -209,6 +209,28 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
var res [][]int
|
||||||
|
func subset(nums []int) [][]int {
|
||||||
|
res = make([][]int, 0)
|
||||||
|
sort.Ints(nums)
|
||||||
|
Dfs([]int{}, nums, 0)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func Dfs(temp, nums []int, start int){
|
||||||
|
tmp := make([]int, len(temp))
|
||||||
|
copy(tmp, temp)
|
||||||
|
res = append(res, tmp)
|
||||||
|
for i := start; i < len(nums); i++{
|
||||||
|
//if i>start&&nums[i]==nums[i-1]{
|
||||||
|
// continue
|
||||||
|
//}
|
||||||
|
temp = append(temp, nums[i])
|
||||||
|
Dfs(temp, nums, i+1)
|
||||||
|
temp = temp[:len(temp)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
|
|
||||||
|
@ -211,7 +211,30 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
var res[][]int
|
||||||
|
func subsetsWithDup(nums []int)[][]int {
|
||||||
|
res=make([][]int,0)
|
||||||
|
sort.Ints(nums)
|
||||||
|
dfs([]int{},nums,0)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func dfs(temp, num []int, start int) {
|
||||||
|
tmp:=make([]int,len(temp))
|
||||||
|
copy(tmp,temp)
|
||||||
|
|
||||||
|
res=append(res,tmp)
|
||||||
|
for i:=start;i<len(num);i++{
|
||||||
|
if i>start&&num[i]==num[i-1]{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
temp=append(temp,num[i])
|
||||||
|
dfs(temp,num,i+1)
|
||||||
|
temp=temp[:len(temp)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -273,7 +273,25 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func isValidBST(root *TreeNode) bool {
|
||||||
|
if root == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return isBST(root, math.MinInt64, math.MaxFloat64)
|
||||||
|
}
|
||||||
|
func isBST(root *TreeNode, min, max int) bool {
|
||||||
|
if root == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if min >= root.Val || max <= root.Val {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -253,7 +253,6 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
|
||||||
@ -284,4 +283,4 @@ const check = (leftPtr, rightPtr) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -353,7 +353,6 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -384,7 +383,40 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func isBalanced(root *TreeNode) bool {
|
||||||
|
if root==nil{
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !isBalanced(root.Left) || !isBalanced(root.Right){
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
LeftH:=maxdepth(root.Left)+1
|
||||||
|
RightH:=maxdepth(root.Right)+1
|
||||||
|
if abs(LeftH-RightH)>1{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
func maxdepth(root *TreeNode)int{
|
||||||
|
if root==nil{
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return max(maxdepth(root.Left),maxdepth(root.Right))+1
|
||||||
|
}
|
||||||
|
func max(a,b int)int{
|
||||||
|
if a>b{
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
func abs(a int)int{
|
||||||
|
if a<0{
|
||||||
|
return -a
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,6 +117,33 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func rob(nums []int) int {
|
||||||
|
if len(nums)<1{
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if len(nums)==1{
|
||||||
|
return nums[0]
|
||||||
|
}
|
||||||
|
if len(nums)==2{
|
||||||
|
return max(nums[0],nums[1])
|
||||||
|
}
|
||||||
|
dp :=make([]int,len(nums))
|
||||||
|
dp[0]=nums[0]
|
||||||
|
dp[1]=max(nums[0],nums[1])
|
||||||
|
for i:=2;i<len(nums);i++{
|
||||||
|
dp[i]=max(dp[i-2]+nums[i],dp[i-1])
|
||||||
|
}
|
||||||
|
return dp[len(dp)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a, b int) int {
|
||||||
|
if a>b{
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,7 +84,28 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public boolean isHappy(int n) {
|
||||||
|
Set<Integer> record = new HashSet<>();
|
||||||
|
while (n != 1 && !record.contains(n)) {
|
||||||
|
record.add(n);
|
||||||
|
n = getNextNumber(n);
|
||||||
|
}
|
||||||
|
return n == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNextNumber(int n) {
|
||||||
|
int res = 0;
|
||||||
|
while (n > 0) {
|
||||||
|
int temp = n % 10;
|
||||||
|
res += temp * temp;
|
||||||
|
n = n / 10;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -225,9 +225,22 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func invertTree(root *TreeNode) *TreeNode {
|
||||||
|
if root ==nil{
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
temp:=root.Left
|
||||||
|
root.Left=root.Right
|
||||||
|
root.Right=temp
|
||||||
|
|
||||||
|
invertTree(root.Left)
|
||||||
|
invertTree(root.Right)
|
||||||
|
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,19 +88,19 @@ Java:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean isAnagram(String s, String t) {
|
public boolean isAnagram(String s, String t) {
|
||||||
int[] strMap = new int[123];
|
|
||||||
for (int i = 0; i < s.length(); i++) {
|
|
||||||
strMap[s.charAt(i) + 0]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < t.length(); i++) {
|
int[] record = new int[26];
|
||||||
strMap[t.charAt(i) + 0]--;
|
for (char c : s.toCharArray()) {
|
||||||
|
record[c - 'a'] += 1;
|
||||||
}
|
}
|
||||||
|
for (char c : t.toCharArray()) {
|
||||||
for (int i = 90; i < 123; i++) {
|
record[c - 'a'] -= 1;
|
||||||
if (strMap[i] != 0) return false;
|
}
|
||||||
|
for (int i : record) {
|
||||||
|
if (i != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,6 +160,17 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func reverseString(s []byte) {
|
||||||
|
left:=0
|
||||||
|
right:=len(s)-1
|
||||||
|
for left<right{
|
||||||
|
s[left],s[right]=s[right],s[left]
|
||||||
|
left++
|
||||||
|
right--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,6 +75,7 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -159,23 +159,51 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
**递归**
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int sumOfLeftLeaves(TreeNode root) {
|
public int sumOfLeftLeaves(TreeNode root) {
|
||||||
if (root == null) return 0;
|
if (root == null) return 0;
|
||||||
int sum = 0;
|
int leftValue = sumOfLeftLeaves(root.left); // 左
|
||||||
if (root.left != null && root.left.left == null && root.left.right == null) {
|
int rightValue = sumOfLeftLeaves(root.right); // 右
|
||||||
sum = root.left.val;
|
|
||||||
|
int midValue = 0;
|
||||||
|
if (root.left != null && root.left.left == null && root.left.right == null) { // 中
|
||||||
|
midValue = root.left.val;
|
||||||
}
|
}
|
||||||
int left = sumOfLeftLeaves(root.left);
|
int sum = midValue + leftValue + rightValue;
|
||||||
int right = sumOfLeftLeaves(root.right);
|
return sum;
|
||||||
return left + right + sum;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**迭代**
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int sumOfLeftLeaves(TreeNode root) {
|
||||||
|
if (root == null) return 0;
|
||||||
|
Stack<TreeNode> stack = new Stack<> ();
|
||||||
|
stack.add(root);
|
||||||
|
int result = 0;
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
TreeNode node = stack.pop();
|
||||||
|
if (node.left != null && node.left.left == null && node.left.right == null) {
|
||||||
|
result += node.left.val;
|
||||||
|
}
|
||||||
|
if (node.right != null) stack.add(node.right);
|
||||||
|
if (node.left != null) stack.add(node.left);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
|
||||||
@ -188,4 +216,6 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
|
||||||
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
|
||||||
|
@ -284,6 +284,44 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func deleteNode(root *TreeNode, key int) *TreeNode {
|
||||||
|
if root==nil{
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if key<root.Val{
|
||||||
|
root.Left=deleteNode(root.Left,key)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
if key>root.Val{
|
||||||
|
root.Right=deleteNode(root.Right,key)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
if root.Right==nil{
|
||||||
|
return root.Left
|
||||||
|
}
|
||||||
|
if root.Left==nil{
|
||||||
|
return root.Right
|
||||||
|
}
|
||||||
|
minnode:=root.Right
|
||||||
|
for minnode.Left!=nil{
|
||||||
|
minnode=minnode.Left
|
||||||
|
}
|
||||||
|
root.Val=minnode.Val
|
||||||
|
root.Right=deleteNode1(root.Right)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteNode1(root *TreeNode)*TreeNode{
|
||||||
|
if root.Left==nil{
|
||||||
|
pRight:=root.Right
|
||||||
|
root.Right=nil
|
||||||
|
return pRight
|
||||||
|
}
|
||||||
|
root.Left=deleteNode1(root.Left)
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -163,10 +163,6 @@ void backtracking(参数) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
|
@ -131,9 +131,6 @@
|
|||||||
最后,大家周末愉快!
|
最后,大家周末愉快!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
<p align="center"><strong>欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
<p align="center"><strong>欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 数组理论基础
|
## 数组理论基础
|
||||||
|
|
||||||
数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力
|
数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力
|
||||||
@ -23,6 +24,8 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
需要两点注意的是
|
需要两点注意的是
|
||||||
|
|
||||||
* **数组下标都是从0开始的。**
|
* **数组下标都是从0开始的。**
|
||||||
@ -34,6 +37,7 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。
|
而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。
|
||||||
|
|
||||||
**数组的元素是不能删的,只能覆盖。**
|
**数组的元素是不能删的,只能覆盖。**
|
||||||
@ -42,110 +46,80 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
**那么二维数组在内存的空间地址是连续的么?**
|
**那么二维数组在内存的空间地址是连续的么?**
|
||||||
|
|
||||||
不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的,如图:
|
不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的。
|
||||||
|
|
||||||
|
我们来做一个实验,C++测试代码如下:
|
||||||
|
|
||||||
|
```C++
|
||||||
|
void test_arr() {
|
||||||
|
int array[2][3] = {
|
||||||
|
{0, 1, 2},
|
||||||
|
{3, 4, 5}
|
||||||
|
};
|
||||||
|
cout << &array[0][0] << " " << &array[0][1] << " " << &array[0][2] << endl;
|
||||||
|
cout << &array[1][0] << " " << &array[1][1] << " " << &array[1][2] << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
test_arr();
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
测试地址为
|
||||||
|
|
||||||
|
```
|
||||||
|
0x7ffee4065820 0x7ffee4065824 0x7ffee4065828
|
||||||
|
0x7ffee406582c 0x7ffee4065830 0x7ffee4065834
|
||||||
|
```
|
||||||
|
|
||||||
|
注意地址为16进制,可以看出二维数组地址是连续一条线的。
|
||||||
|
|
||||||
|
一些录友可能看不懂内存地址,我就简单介绍一下, 0x7ffee4065820 与 0x7ffee4065824 差了一个4,就是4个字节,因为这是一个int型的数组,所以两个相信数组元素地址差4个字节。
|
||||||
|
|
||||||
|
0x7ffee4065828 与 0x7ffee406582c 也是差了4个字节,在16进制里8 + 4 = c,c就是12。
|
||||||
|
|
||||||
|
如图:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Java的二维数组可能是如下排列的方式:
|
**所以可以看出在C++中二维数组在地址空间上是连续的**。
|
||||||
|
|
||||||
|
像Java是没有指针的,同时也不对程序员暴漏其元素的地址,寻址操作完全交给虚拟机。
|
||||||
|
|
||||||
|
所以看不到每个元素的地址情况,这里我以Java为例,也做一个实验。
|
||||||
|
|
||||||
|
```Java
|
||||||
|
public static void test_arr() {
|
||||||
|
int[][] arr = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}, {9,9,9}};
|
||||||
|
System.out.println(arr[0]);
|
||||||
|
System.out.println(arr[1]);
|
||||||
|
System.out.println(arr[2]);
|
||||||
|
System.out.println(arr[3]);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
输出的地址为:
|
||||||
|
|
||||||
|
```
|
||||||
|
[I@7852e922
|
||||||
|
[I@4e25154f
|
||||||
|
[I@70dea4e
|
||||||
|
[I@5c647e05
|
||||||
|
```
|
||||||
|
|
||||||
|
这里的数值也是16进制,这不是真正的地址,而是经过处理过后的数值了,我们也可以看出,二维数组的每一行头结点的地址是没有规则的,更谈不上连续。
|
||||||
|
|
||||||
|
所以Java的二维数组可能是如下排列的方式:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
我们在[数组过于简单,但你该了解这些!](https://mp.weixin.qq.com/s/c2KABb-Qgg66HrGf8z-8Og)分别作了实验
|
这里面试中数组相关的理论知识就介绍完了。
|
||||||
|
|
||||||
## 数组的经典题目
|
后续我将介绍面试中数组相关的五道经典面试题目,敬请期待!
|
||||||
|
|
||||||
在面试中,数组是必考的基础数据结构。
|
|
||||||
|
|
||||||
其实数据的题目在思想上一般比较简单的,但是如果想高效,并不容易。
|
|
||||||
|
|
||||||
我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。
|
|
||||||
|
|
||||||
### 二分法
|
|
||||||
|
|
||||||
[704.二分查找](https://mp.weixin.qq.com/s/4X-8VRgnYRGd5LYGZ33m4w)
|
|
||||||
|
|
||||||
在这道题目中我们讲到了**循环不变量原则**,只有在循环中坚持对区间的定义,才能清楚的把握循环中的各种细节。
|
|
||||||
|
|
||||||
**二分法是算法面试中的常考题,建议通过这道题目,锻炼自己手撕二分的能力**。
|
|
||||||
|
|
||||||
相关题目:
|
|
||||||
|
|
||||||
* 35.搜索插入位置
|
|
||||||
* 34.在排序数组中查找元素的第一个和最后一个位置
|
|
||||||
* 69.x 的平方根
|
|
||||||
* 367.有效的完全平方数
|
|
||||||
|
|
||||||
### 双指针法
|
|
||||||
|
|
||||||
[27. 移除元素](https://mp.weixin.qq.com/s/RMkulE4NIb6XsSX83ra-Ww)
|
|
||||||
|
|
||||||
双指针法(快慢指针法):**通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
|
|
||||||
|
|
||||||
暴力解法时间复杂度:O(n^2)
|
|
||||||
双指针时间复杂度:O(n)
|
|
||||||
|
|
||||||
这道题目迷惑了不少同学,纠结于数组中的元素为什么不能删除,主要是因为一下两点:
|
|
||||||
|
|
||||||
* 数组在内存中是连续的地址空间,不能释放单一元素,如果要释放,就是全释放(程序运行结束,回收内存栈空间)。
|
|
||||||
* C++中vector和array的区别一定要弄清楚,vector的底层实现是array,所以vector展现出友好的一些都是因为经过包装了。
|
|
||||||
|
|
||||||
双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组和链表操作的面试题,都使用双指针法。
|
|
||||||
|
|
||||||
相关题目:
|
|
||||||
|
|
||||||
* 26.删除排序数组中的重复项
|
|
||||||
* 283.移动零
|
|
||||||
* 844.比较含退格的字符串
|
|
||||||
* 977.有序数组的平方
|
|
||||||
|
|
||||||
### 滑动窗口
|
|
||||||
|
|
||||||
[209.长度最小的子数组](https://mp.weixin.qq.com/s/ewCRwVw0h0v4uJacYO7htQ)
|
|
||||||
|
|
||||||
本题介绍了数组操作中的另一个重要思想:滑动窗口。
|
|
||||||
|
|
||||||
暴力解法时间复杂度:O(n^2)
|
|
||||||
滑动窗口时间复杂度:O(n)
|
|
||||||
|
|
||||||
本题中,主要要理解滑动窗口如何移动 窗口起始位置,达到动态更新窗口大小的,从而得出长度最小的符合条件的长度。
|
|
||||||
|
|
||||||
**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)的暴力解法降为O(n)。**
|
|
||||||
|
|
||||||
如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。
|
|
||||||
|
|
||||||
相关题目:
|
|
||||||
|
|
||||||
* 904.水果成篮
|
|
||||||
* 76.最小覆盖子串
|
|
||||||
|
|
||||||
### 模拟行为
|
|
||||||
|
|
||||||
[59.螺旋矩阵II](https://mp.weixin.qq.com/s/Hn6-mlCPvKAdWbiFfQyaaw)
|
|
||||||
|
|
||||||
模拟类的题目在数组中很常见,不涉及到什么算法,就是单纯的模拟,十分考察大家对代码的掌控能力。
|
|
||||||
|
|
||||||
在这道题目中,我们再一次介绍到了**循环不变量原则**,其实这也是写程序中的重要原则。
|
|
||||||
|
|
||||||
相信大家又遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,踩了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。
|
|
||||||
|
|
||||||
相关题目:
|
|
||||||
|
|
||||||
* 54.螺旋矩阵
|
|
||||||
* 剑指Offer 29.顺时针打印矩阵
|
|
||||||
|
|
||||||
## 总结
|
|
||||||
|
|
||||||
从二分法到双指针,从滑动窗口到螺旋矩阵,相信如果大家真的认真做了「代码随想录」每日推荐的题目,定会有所收获。
|
|
||||||
|
|
||||||
**每道题目后面都有相关练习题,也别忘了去做做!**
|
|
||||||
|
|
||||||
数组专题中讲解和相关题目已经有16道了,就不介绍太过题目了,因为数组是非常基础的数据结构后面很多专题还会用到数组,所以后面的题目依然会会间接练习数组的。
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user