22年37周

一:左旋转字符串

题目链接:左旋转字符串

思路:比较简单的一种是使用额外的空间进行反转

节省空间的方法是:先对前n个字符进行反转,再对后n个字符进行反转,再对整个字符进行反转

//常规方法
func reverseLeftWords(s string, n int) string {
    bytes := []byte(s)
    qian := bytes[n:]
    qian = append(qian,bytes[:n]...)
    return string(qian)
}



//节省空间写法

func reverseLeftWords(s string, n int) string {

    if n == len(s) {
        return s 
    }

    res := []byte(s)

    reverseSubStr(res[:n])
    reverseSubStr(res[n:])
    reverseSubStr(res)

    return string(res)
}

func reverseSubStr (str []byte) {

    i,j := 0,len(str)-1

    for i < j {
        temp := str[i]
        str[i] = str[j]
        str[j] = temp
        i++
        j--
    }
}

二、实现 strStr()

题目链接:实现 strStr()

思路:这是一道典型的字符串匹配算法,可以使用一种比较简单的字符串匹配算法,或者比较复杂的KMP算法,区别在于,当有子串不匹配时,是从头开始匹配字串,还是从有记录那块开始。我使用的简单的字符串匹配算法。

func strStr(haystack string, needle string) int {

    hl,nl := len(haystack),len(needle)//两者长度

    if nl == 0 || nl > hl {
        return -1
    }

    hi,hj := 0,0 //记录haystack中字串开始的地方
    ni := 0 //记录needle中指针所指向的位置

    for hl - hi >= nl {
        
        //开始比对
        for ni < nl {
            //相等的话,指针后移
            if haystack[hj] == needle[ni] {

                hj++
                ni++
            } else {
            //不相等的话,则前面的指针加一,后面的指针归位
                ni = 0
                hi++
                hj = hi
                break
            }
           
            //说明比较到了最后一个
            if ni == nl {
                
                return hi
            }
        }
    }
    return -1

}

三、重复的子字符串

题目链接:重复的子字符串

思路:可以用字符串匹配算法来实现,因为假如有子串可以重复n次组成母串的话,说明,从0开始比较一定能比较出来,之后重复开始向后比较。

不过我是调用库函数实现的本题,和这个思路不太一样。

import "strings"

func repeatedSubstringPattern(s string) bool {

    for i:=1;i<=len(s)/2;i++ {
        subStr := s[:i]
        if "" == strings.Replace(s,subStr,"",-1) {
            return true
        } 
    }
    return false
}

四:用栈实现队列

连接:用栈实现队列

思路:用两个栈,其中专门用来存放进来的元素,另一个用来存放出去的元素,当入队列时,直接往入栈里面放就行了。出队列时,先判断出栈是否为空,不空的话,直接从出栈里面拿出一个元素。空的话,先把入栈的元素全部都挪移到出栈,在从出栈出来。

type MyQueue struct {
    inStack [100]int
    inIndex int
    outStack [100]int
    outIndex int
}


func Constructor() MyQueue {
    queue := MyQueue{[100]int{},0,[100]int{},-1}
    return queue
}


func (this *MyQueue) Push(x int)  {
    this.inStack[this.inIndex] = x
    this.inIndex += 1
}


func (this *MyQueue) Pop() int {
    if this.outIndex != -1 {//出队列不为空
        res := this.outStack[this.outIndex]
        this.outIndex--
        return res
    }
    //出队列为空时
    for this.inIndex > 0 {
        this.inIndex--
        this.outIndex++
        this.outStack[this.outIndex] = this.inStack[this.inIndex]
    }
    res := this.outStack[this.outIndex]
    this.outIndex--
    return res
}


func (this *MyQueue) Peek() int {
    if this.outIndex != -1 {//出队列不为空
        return this.outStack[this.outIndex]
    }
    //出队列为空时
    for this.inIndex > 0 {
        this.inIndex--
        this.outIndex++
        this.outStack[this.outIndex] = this.inStack[this.inIndex]
    }
    return this.outStack[this.outIndex]
}


func (this *MyQueue) Empty() bool {
    if this.inIndex == 0 && this.outIndex == -1 {
        return true
    }
    return false
}


/**
 * Your MyQueue object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Push(x);
 * param_2 := obj.Pop();
 * param_3 := obj.Peek();
 * param_4 := obj.Empty();
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值