diff --git a/.idea/LeetCode-Go.iml b/.idea/LeetCode-Go.iml
new file mode 100644
index 000000000..c956989b2
--- /dev/null
+++ b/.idea/LeetCode-Go.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/leetcode/editor.xml b/.idea/leetcode/editor.xml
new file mode 100644
index 000000000..d802b6ed5
--- /dev/null
+++ b/.idea/leetcode/editor.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 000000000..1a212c1a7
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 000000000..94a25f7f4
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 000000000..35cda1095
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,171 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/leetcode/0001.Two-Sum/1. Two Sum copy.go b/leetcode/0001.Two-Sum/1. Two Sum copy.go
new file mode 100644
index 000000000..e902a4a73
--- /dev/null
+++ b/leetcode/0001.Two-Sum/1. Two Sum copy.go
@@ -0,0 +1,14 @@
+package leetcode
+
+func twoSum1(num []int, target int) []int {
+ m := make(map[int]int)
+ for i, a := range num {
+ if idx, ok := m[target-a]; ok {
+ return []int{idx, i}
+ }
+ m[a] = i
+ }
+ return nil
+}
+
+
diff --git a/leetcode/0001.Two-Sum/1. Two Sum_test.go b/leetcode/0001.Two-Sum/1. Two Sum_test.go
index e6378be08..b529c125c 100644
--- a/leetcode/0001.Two-Sum/1. Two Sum_test.go
+++ b/leetcode/0001.Two-Sum/1. Two Sum_test.go
@@ -57,7 +57,8 @@ func Test_Problem1(t *testing.T) {
for _, q := range qs {
_, p := q.ans1, q.para1
- fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum(p.nums, p.target))
+ // fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum(p.nums, p.target))
+ fmt.Printf("【input】:%v 【output】:%v\n", p, twoSum1(p.nums, p.target))
}
fmt.Printf("\n\n\n")
}
diff --git a/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers copy.go b/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers copy.go
new file mode 100644
index 000000000..e7cda7231
--- /dev/null
+++ b/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers copy.go
@@ -0,0 +1,42 @@
+package leetcode
+
+import (
+ "github.com/halfrost/LeetCode-Go/structures"
+)
+
+// ListNode define
+type ListNode1 = structures.ListNode
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ * Val int
+ * Next *ListNode
+ * }
+ */
+
+func addTwoNumbers1(l1 *ListNode, l2 *ListNode) *ListNode {
+ head := &ListNode{Val: 0}
+ n1, n2, carrier, current := 0, 0, 0, head
+ for l1 != nil || l2 != nil || carrier != 0 {
+ if l1 == nil {
+ n1 = 0
+ } else {
+ n1 = l1.Val
+ l1 = l1.Next
+ }
+ if l2 == nil {
+ n2 = 0
+ } else {
+ n2 = l2.Val
+ l2 = l2.Next
+ }
+
+ current.Next = &ListNode{Val: (n1 + n2 + carrier) % 10}
+ carrier = (n1 + n2 + carrier) / 10
+ current = current.Next
+
+ }
+
+ return head.Next
+}
diff --git a/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers_test.go b/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers_test.go
index d615065b5..822aaec0d 100644
--- a/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers_test.go
+++ b/leetcode/0002.Add-Two-Numbers/2. Add Two Numbers_test.go
@@ -75,7 +75,7 @@ func Test_Problem2(t *testing.T) {
for _, q := range qs {
_, p := q.ans2, q.para2
- fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers(structures.Ints2List(p.one), structures.Ints2List(p.another))))
+ fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(addTwoNumbers1(structures.Ints2List(p.one), structures.Ints2List(p.another))))
}
fmt.Printf("\n\n\n")
}
diff --git a/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters copy.go b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters copy.go
new file mode 100644
index 000000000..b931ff479
--- /dev/null
+++ b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters copy.go
@@ -0,0 +1,93 @@
+package leetcode
+
+
+func lengthOfLongestSubstringV0(s string) int {
+ if len(s) == 0 {
+ return 0
+ }
+
+ position := make(map[string]bool)
+ left, right, result := 0, 0, 0
+ for left <= len(s) {
+ var l, r string
+ r = string(s[right])
+ l = string(s[left])
+
+ if position[r] {
+ left++
+ position[l] = false
+ } else {
+ right++
+ position[r] = true
+ }
+
+ result = max(result, right - left)
+
+ //终止条件
+ if left + result >= len(s) {
+ break
+ }
+ }
+
+ return result
+}
+
+
+
+
+
+
+
+// 解法一 位图
+func lengthOfLongestSubstringV1(s string) int {
+ if len(s) == 0 {
+ return 0
+ }
+ var bitSet [256]bool
+ result, left, right := 0, 0, 0
+ for left < len(s) {
+ // 右侧字符对应的bitSet被标记true,说明此字符在X位置重复,需要左侧向前移动,直到将X标记为false
+ if bitSet[s[right]] {
+ bitSet[s[left]] = false
+ left++
+ } else {
+ bitSet[s[right]] = true
+ right++
+ }
+ if result < right-left {
+ result = right - left
+ }
+ if left+result >= len(s) || right >= len(s) {
+ break
+ }
+ }
+ return result
+}
+
+// 解法二 滑动窗口
+func lengthOfLongestSubstringV2(s string) int {
+ if len(s) == 0 {
+ return 0
+ }
+ var freq [256]int
+ result, left, right := 0, 0, -1
+
+ for left < len(s) {
+ if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
+ freq[s[right+1]-'a']++
+ right++
+ } else {
+ freq[s[left]-'a']--
+ left++
+ }
+ result = maxV1(result, right-left+1)
+ }
+ return result
+}
+
+func maxV1(a int, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
diff --git a/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go
index 7eebdbfa8..00cd94c98 100644
--- a/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go
+++ b/leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go
@@ -45,6 +45,10 @@ func Test_Problem3(t *testing.T) {
para3{""},
ans3{0},
},
+ {
+ para3{"12341234"},
+ ans3{4},
+ },
}
fmt.Printf("------------------------Leetcode Problem 3------------------------\n")
diff --git a/leetcode/0100.Same-Tree/100. Same Tree copy.go b/leetcode/0100.Same-Tree/100. Same Tree copy.go
new file mode 100644
index 000000000..344eac6f8
--- /dev/null
+++ b/leetcode/0100.Same-Tree/100. Same Tree copy.go
@@ -0,0 +1,34 @@
+package leetcode
+
+import (
+ "github.com/halfrost/LeetCode-Go/structures"
+)
+
+// TreeNode define
+type TreeNode1 = structures.TreeNode
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ * Val int
+ * Left *TreeNode
+ * Right *TreeNode
+ * }
+ */
+
+func isSameTree1(p *TreeNode, q *TreeNode) bool {
+ if p == nil && q == nil {
+ return true
+ } else if p != nil && q != nil {
+ if p.Val != q.Val {
+ return false
+ }
+
+ return isSameTree1(p.Left, q.Left) &&
+ isSameTree1(p.Right, q.Right)
+ } else {
+ return false
+ }
+}
+
+
diff --git a/leetcode/0136.Single-Number/136. Single Number copy.go b/leetcode/0136.Single-Number/136. Single Number copy.go
new file mode 100644
index 000000000..8b9558e4a
--- /dev/null
+++ b/leetcode/0136.Single-Number/136. Single Number copy.go
@@ -0,0 +1,9 @@
+package leetcode
+
+func singleNumberV2(nums []int) int {
+ result := 0
+ for i := 0; i < len(nums); i++ {
+ result ^= nums[i]
+ }
+ return result
+}