Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24e98f4

Browse files
committed
fixed content
1 parent1d9fc2b commit24e98f4

17 files changed

+370
-42
lines changed

‎100. Same Tree.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎100. [E]Same Tree.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
/**
4+
Given two binary trees, write a function to check if they are the same or not.
5+
6+
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
7+
8+
Example 1:
9+
10+
Input: 1 1
11+
/ \ / \
12+
2 3 2 3
13+
14+
[1,2,3], [1,2,3]
15+
16+
Output: true
17+
Example 2:
18+
19+
Input: 1 1
20+
/ \
21+
2 2
22+
23+
[1,2], [1,null,2]
24+
25+
Output: false
26+
Example 3:
27+
28+
Input: 1 1
29+
/ \ / \
30+
2 1 1 2
31+
32+
[1,2,1], [1,1,2]
33+
34+
Output: false
35+
*/
36+
typeTreeNodestruct {
37+
Valint
38+
Left*TreeNode
39+
Right*TreeNode
40+
}
41+
42+
funcisSameTree(p*TreeNode,q*TreeNode)bool {
43+
44+
ifp==nil&&q==nil {
45+
returntrue
46+
}
47+
if (p==nil&&q!=nil)|| (p!=nil&&q==nil) {
48+
returnfalse
49+
}
50+
ifp.Val!=q.Val {
51+
returnfalse
52+
}
53+
returnisSameTree(p.Left,q.Left)&&isSameTree(p.Right,q.Right)
54+
}

‎101. Symmetric Tree.gorenamed to‎101. [E]Symmetric Tree.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
package main
22

3+
/**
4+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
5+
6+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
7+
8+
1
9+
/ \
10+
2 2
11+
/ \ / \
12+
3 4 4 3
13+
14+
15+
But the following [1,2,2,null,3,null,3] is not:
16+
17+
1
18+
/ \
19+
2 2
20+
\ \
21+
3 3
22+
*/
23+
324
typeTreeNodestruct {
425
Valint
526
Left*TreeNode

‎105. Construct Binary Tree from Preorder and Inorder Traversal.gorenamed to‎105. [M]Construct Binary Tree from Preorder and Inorder Traversal.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package main
22

3+
/**
4+
Given preorder and inorder traversal of a tree, construct the binary tree.
5+
6+
Note:
7+
You may assume that duplicates do not exist in the tree.
8+
9+
For example, given
10+
11+
preorder = [3,9,20,15,7]
12+
inorder = [9,3,15,20,7]
13+
Return the following binary tree:
14+
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
21+
*/
322
typeTreeNodestruct {
423
Valint
524
Left*TreeNode

‎78. Subsets.gorenamed to‎78. [M]Subsets.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package main
22

3+
/**
4+
Given a set of distinct integers, nums, return all possible subsets (the power set).
5+
6+
Note: The solution set must not contain duplicate subsets.
7+
8+
Example:
9+
10+
Input: nums = [1,2,3]
11+
Output:
12+
[
13+
[3],
14+
[1],
15+
[2],
16+
[1,2,3],
17+
[1,3],
18+
[2,3],
19+
[1,2],
20+
[]
21+
]
22+
*/
323
/**
424
combination:
525
dfs方法,注意中间结果一定要copy到新的变量中,不然会得到空值

‎79. Word Search.gorenamed to‎79. [M]Word Search.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package main
22

3+
/**
4+
Given a 2D board and a word, find if the word exists in the grid.
5+
6+
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
7+
8+
Example:
9+
10+
board =
11+
[
12+
['A','B','C','E'],
13+
['S','F','C','S'],
14+
['A','D','E','E']
15+
]
16+
17+
Given word = "ABCCED", return true.
18+
Given word = "SEE", return true.
19+
Given word = "ABCB", return false.
20+
*/
21+
322
funchelp(b [][]byte,wordstring,i,jint)bool {
423
ifword=="" {
524
returntrue

‎81. Search in Rotated Sorted Array II.gorenamed to‎81. [M]Search in Rotated Sorted Array II.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package main
22

3+
/**
4+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5+
6+
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
7+
8+
You are given a target value to search. If found in the array return true, otherwise return false.
9+
10+
Example 1:
11+
12+
Input: nums = [2,5,6,0,0,1,2], target = 0
13+
Output: true
14+
Example 2:
15+
16+
Input: nums = [2,5,6,0,0,1,2], target = 3
17+
Output: false
18+
*/
19+
320
funcsearch(nums []int,targetint)bool {
421
l:=len(nums)
522
ifl==0 {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
/**
4+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
5+
6+
Example 1:
7+
8+
Input: 1->2->3->3->4->4->5
9+
Output: 1->2->5
10+
Example 2:
11+
12+
Input: 1->1->1->2->3
13+
Output: 2->3
14+
*/
15+
typeListNodestruct {
16+
Valint
17+
Next*ListNode
18+
}
19+
20+
funcdeleteDuplicates(head*ListNode)*ListNode {
21+
t:=&ListNode{}
22+
t.Next=head
23+
p:=t
24+
forp.Next!=nil&&p.Next.Next!=nil {
25+
ifp.Next.Val==p.Next.Next.Val {
26+
dup:=p.Next.Val
27+
forp.Next!=nil&&p.Next.Val==dup {
28+
p.Next=p.Next.Next
29+
}
30+
}else {
31+
p=p.Next
32+
}
33+
}
34+
returnt.Next
35+
}

‎83. Remove Duplicates from Sorted List.gorenamed to‎83. [E]Remove Duplicates from Sorted List.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package main
22

3+
/**
4+
Given a sorted linked list, delete all duplicates such that each element appear only once.
5+
6+
Example 1:
7+
8+
Input: 1->1->2
9+
Output: 1->2
10+
Example 2:
11+
12+
Input: 1->1->2->3->3
13+
Output: 1->2->3
14+
*/
15+
316
typeListNodestruct {
417
Valint
518
Next*ListNode

‎86. Partition List.gorenamed to‎86. [M]Partition List.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package main
22

3+
/**
4+
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
5+
6+
You should preserve the original relative order of the nodes in each of the two partitions.
7+
8+
Example:
9+
10+
Input: head = 1->4->3->2->5->2, x = 3
11+
Output: 1->2->2->4->3->5
12+
*/
13+
314
typeListNodestruct {
415
Valint
516
Next*ListNode

‎88. Merge Sorted Array.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎88. [E]Merge Sorted Array.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
/**
4+
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
5+
6+
Note:
7+
8+
The number of elements initialized in nums1 and nums2 are m and n respectively.
9+
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.
10+
Example:
11+
12+
Input:
13+
nums1 = [1,2,3,0,0,0], m = 3
14+
nums2 = [2,5,6], n = 3
15+
16+
Output: [1,2,2,3,5,6]
17+
*/
18+
19+
funcmerge(nums1 []int,mint,nums2 []int,nint) {
20+
form>0||n>0 {
21+
ifn==0 {
22+
break
23+
}
24+
ifm==0 {
25+
nums1[n-1]=nums2[n-1]
26+
n--
27+
continue
28+
}
29+
ifnums1[m-1]>nums2[n-1] {
30+
nums1[m+n-1]=nums1[m-1]
31+
m--
32+
}else {
33+
nums1[m+n-1]=nums2[n-1]
34+
n--
35+
}
36+
}
37+
}

‎89. [M]Gray Code.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
/**
4+
The gray code is a binary numeral system where two successive values differ in only one bit.
5+
6+
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
7+
8+
Example 1:
9+
10+
Input: 2
11+
Output: [0,1,3,2]
12+
Explanation:
13+
00 - 0
14+
01 - 1
15+
11 - 3
16+
10 - 2
17+
18+
For a given n, a gray code sequence may not be uniquely defined.
19+
For example, [0,2,3,1] is also a valid gray code sequence.
20+
21+
00 - 0
22+
10 - 2
23+
11 - 3
24+
01 - 1
25+
Example 2:
26+
27+
Input: 0
28+
Output: [0]
29+
Explanation: We define the gray code sequence to begin with 0.
30+
A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
31+
Therefore, for n = 0 the gray code sequence is [0].
32+
*/
33+
34+
funcgrayCode(nint) []int {
35+
res:= []int{0}
36+
fori:=0;i<n;i++ {
37+
h:=1<<uint(i)
38+
size:=len(res)
39+
forj:=size-1;j>=0;j-- {
40+
num:=res[j]
41+
num+=h
42+
res=append(res,num)
43+
}
44+
}
45+
returnres
46+
}

‎92. Reverse Linked List II.gorenamed to‎92. [M]Reverse Linked List II.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package main
22

3+
/**
4+
Reverse a linked list from position m to n. Do it in one-pass.
5+
6+
Note: 1 ≤ m ≤ n ≤ length of list.
7+
8+
Example:
9+
10+
Input: 1->2->3->4->5->NULL, m = 2, n = 4
11+
Output: 1->4->3->2->5->NULL
12+
*/
313
typeListNodestruct {
414
Valint
515
Next*ListNode

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp