Skip to content
Merged
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
34 changes: 34 additions & 0 deletions src/0001_two_sum/twosum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
1. Two Sum

Source: https://leetcode.com/problems/two-sum/

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

*/

package twosum

// Time complexity: O(n)
// Space complexity: O(n)
func twoSum(nums []int, target int) []int {
record := make(map[int]int)

for i, j := range nums {
complement := target - j
if res, ok := record[complement]; ok {
return []int{res, i}
}
record[j] = i
}
return []int{}
}
18 changes: 18 additions & 0 deletions src/0001_two_sum/twosum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package twosum

import (
"reflect"
"testing"
)

func TestTwoSum(t *testing.T) {
nums := []int{2, 7, 11, 15}

if res := twoSum(nums, 9); !reflect.DeepEqual(res, []int{0, 1}) {
t.Error("Failed, two sum")
}

if res := twoSum(nums, 6); !reflect.DeepEqual(res, []int{}) {
t.Error("Failed, two sum")
}
}
96 changes: 96 additions & 0 deletions src/0002_add_two_numbers/add_two_numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
2. Add Two Numbers

Source: https://leetcode.com/problems/add-two-numbers/

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
*/

package addtwonumbers

// ListNode is node of linked list
type ListNode struct {
Val int
Next *ListNode
}

// 解法一,暴力解法
func addTwoNumbers1(l1 *ListNode, l2 *ListNode) *ListNode {
head := ListNode{Val: -1}
cur := &head
carry := 0
for l1 != nil && l2 != nil {
sum := l1.Val + l2.Val + carry
val := sum % 10
carry = sum / 10
node := ListNode{Val: val}
cur.Next = &node
cur = cur.Next
l1 = l1.Next
l2 = l2.Next
}
if carry == 0 {
if l1 != nil {
cur.Next = l1
}
if l2 != nil {
cur.Next = l2
}
return head.Next
}
for l1 != nil {
sum := l1.Val + carry
val := sum % 10
carry = sum / 10
node := ListNode{Val: val}
cur.Next = &node
cur = cur.Next
l1 = l1.Next
}
for l2 != nil {
sum := l2.Val + carry
val := sum % 10
carry = sum / 10
node := ListNode{Val: val}
cur.Next = &node
cur = cur.Next
l2 = l2.Next
}
for carry != 0 {
val := carry % 10
carry = carry / 10
node := ListNode{Val: val}
cur.Next = &node
cur = cur.Next
}
return head.Next
}

// 解法二,递归
func addTwoNumbers2(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil && l2 == nil {
return nil
} else if l1 == nil && l2 != nil {
return l2
} else if l1 != nil && l2 == nil {
return l1
}

sum := l1.Val + l2.Val
next := addTwoNumbers2(l1.Next, l2.Next)

if sum >= 10 {
carry := sum / 10
sum %= 10
next = addTwoNumbers2(next, &ListNode{Val: carry})
}
return &ListNode{Val: sum, Next: next}
}
130 changes: 130 additions & 0 deletions src/0002_add_two_numbers/add_two_numbers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package addtwonumbers

import (
"os"
"reflect"
"testing"
)

func createSingleLinkedList(arr []int) *ListNode {
head := &ListNode{}
cur := head

for _, j := range arr {
cur.Next = &ListNode{Val: j}
cur = cur.Next
}
return head.Next
}

func TestAddTwoNumbers1(t *testing.T) {
testDatas := []struct {
name string
arg1 *ListNode
arg2 *ListNode
expected *ListNode
}{
{
name: "one",
arg1: createSingleLinkedList([]int{2, 4, 3}),
arg2: createSingleLinkedList([]int{5, 6, 4}),
expected: createSingleLinkedList([]int{7, 0, 8}),
},
{
name: "two",
arg1: createSingleLinkedList([]int{5}),
arg2: createSingleLinkedList([]int{5}),
expected: createSingleLinkedList([]int{0, 1}),
},
{
name: "three",
arg1: createSingleLinkedList([]int{1}),
arg2: createSingleLinkedList([]int{9, 9, 9}),
expected: createSingleLinkedList([]int{0, 0, 0, 1}),
},
{
name: "four",
arg1: createSingleLinkedList([]int{9, 9, 9}),
arg2: createSingleLinkedList([]int{1}),
expected: createSingleLinkedList([]int{0, 0, 0, 1}),
},
{
name: "five",
arg1: createSingleLinkedList([]int{4, 3, 1}),
arg2: createSingleLinkedList([]int{1}),
expected: createSingleLinkedList([]int{5, 3, 1}),
},
{
name: "six",
arg1: createSingleLinkedList([]int{1}),
arg2: createSingleLinkedList([]int{4, 3, 1}),
expected: createSingleLinkedList([]int{5, 3, 1}),
},
}

for _, testData := range testDatas {
t.Run(testData.name, func(t *testing.T) {
if result := addTwoNumbers1(testData.arg1, testData.arg2); !reflect.DeepEqual(result, testData.expected) {
t.Errorf("expected %v, got %v", testData.expected, result)
}
})
}
}

func TestAddTwoNumbers2(t *testing.T) {
testDatas := []struct {
name string
arg1 *ListNode
arg2 *ListNode
expected *ListNode
}{
{
name: "one",
arg1: createSingleLinkedList([]int{2, 4, 3}),
arg2: createSingleLinkedList([]int{5, 6, 4}),
expected: createSingleLinkedList([]int{7, 0, 8}),
},
{
name: "two",
arg1: createSingleLinkedList([]int{5}),
arg2: createSingleLinkedList([]int{5}),
expected: createSingleLinkedList([]int{0, 1}),
},
{
name: "three",
arg1: createSingleLinkedList([]int{1}),
arg2: createSingleLinkedList([]int{9, 9, 9}),
expected: createSingleLinkedList([]int{0, 0, 0, 1}),
},
{
name: "four",
arg1: createSingleLinkedList([]int{9, 9, 9}),
arg2: createSingleLinkedList([]int{1}),
expected: createSingleLinkedList([]int{0, 0, 0, 1}),
},
{
name: "five",
arg1: createSingleLinkedList([]int{4, 3, 1}),
arg2: createSingleLinkedList([]int{1}),
expected: createSingleLinkedList([]int{5, 3, 1}),
},
{
name: "six",
arg1: createSingleLinkedList([]int{1}),
arg2: createSingleLinkedList([]int{4, 3, 1}),
expected: createSingleLinkedList([]int{5, 3, 1}),
},
}

for _, testData := range testDatas {
t.Run(testData.name, func(t *testing.T) {
if result := addTwoNumbers2(testData.arg1, testData.arg2); !reflect.DeepEqual(result, testData.expected) {
t.Errorf("expected %v, got %v", testData.expected, result)
}
})
}
}

func TestMain(m *testing.M) {
os.Exit(m.Run())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
3. Longest Substring Without Repeating Characters

Source: https://leetcode.com/problems/longest-substring-without-repeating-characters/

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
*/

package longestsubstringwithoutrepeatingcharacters

// Time complexity: O(n)
// Space complexity: O(n)
func lengthOfLongestSubstring(s string) int {

var (
start = 0
res = 0
lastOccurredRecord = make(map[rune]int)
)

for i, ch := range []rune(s) {
if lastIndex, ok := lastOccurredRecord[ch]; ok && lastIndex >= start {
start = lastIndex + 1
}
if i-start+1 > res {
res = i - start + 1
}
lastOccurredRecord[ch] = i
}
return res
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package longestsubstringwithoutrepeatingcharacters

import "testing"

func TestLengthOfLongestSubstring(t *testing.T) {
testData := []string{
"abcabcbb",
"bbbbb",
"pwwkew",
}
expectedData := []int{3, 1, 3}

for index, data := range testData {
if res := lengthOfLongestSubstring(data); res != expectedData[index] {
t.Errorf("expected %d, got %d", expectedData[index], res)
}
}
}
Loading