【LeetCode】Jianzhi Offer <Second Brush>(4)

Table of contents

Topic: Sword Pointing to Offer 09. Using two stacks to implement a queue - LeetCode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! !

Topic: Sword Pointing to Offer 10- I. Fibonacci Sequence- LeetCode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! !

Write at the end:


Topic: Sword Pointing to Offer 09. Using two stacks to implement a queue - LeetCode

The interface of the topic:

type CQueue struct {

}


func Constructor() CQueue {

}


func (this *CQueue) AppendTail(value int)  {

}


func (this *CQueue) DeleteHead() int {

}


/**
 * Your CQueue object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AppendTail(value);
 * param_2 := obj.DeleteHead();
 */

Problem-solving ideas:

When I wrote this question in C++, it was relatively simple and smooth. With STL, I can directly transfer two stacks out of the library, and then use one as inStack and one as OutStack. Put it into OutStack, and then delete the contents of OutStack.

But now using golang to solve this problem, the idea is not difficult, the main thing is how to implement the stack structure elegantly? The feature of slicing is still very useful. It can be said that this time I learned another operation, how to traverse slices in reverse order without subscripts:

this.outStack = append(this.outStack, this.inStack[len(this.inStack)-1])
this.inStack = this.inStack[:len(this.inStack)-1]

Keep getting smaller + keep taking the last element, it's really wonderful. The specific code is as follows: 

code:

type CQueue struct {
    inStack []int
    outStack []int
}


func Constructor() CQueue {
    return CQueue{}
}


func (this *CQueue) AppendTail(value int)  {
    this.inStack = append(this.inStack, value)
}


func (this *CQueue) DeleteHead() int {
    if len(this.outStack) == 0 {
        if len(this.inStack) == 0 {return -1}
        this.in2out()
    }
    value := this.outStack[len(this.outStack)-1]
    this.outStack = this.outStack[:len(this.outStack)-1]
    return value
}

func (this *CQueue) in2out() {
    for len(this.inStack) > 0 {
        this.outStack = append(this.outStack, this.inStack[len(this.inStack)-1])
        this.inStack = this.inStack[:len(this.inStack)-1]
    }
}


/**
 * Your CQueue object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AppendTail(value);
 * param_2 := obj.DeleteHead();
 */

It's over! ! !

Topic: Sword Pointing to Offer 10- I. Fibonacci Sequence- LeetCode

The interface of the topic:

func fib(n int) int {

}

Problem-solving ideas:

This question is a very classic introductory question on dynamic programming. I did it directly here. In fact, space optimization can also be performed, but I don’t think it is necessary. Simple questions are not necessary, and difficult questions can’t be optimized. to be honest.

code:

func fib(n int) int {
    if n <= 1 {return n}
    dp := make([]int, 101)
    dp[0] = 0
    dp[1] = 1
    for i := 2; i <= 100; i++ {
        dp[i] = (dp[i-1] + dp[i-2]) % (1e9 + 7)
    }
    return dp[n] 
}

It's over! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/132618805