美文网首页
Tourist with Data Structure Firs

Tourist with Data Structure Firs

作者: Jiawei_84a5 | 来源:发表于2019-05-18 23:32 被阅读0次

数组和字符串

寻找数组的中心索引.

本来使用暴力破解方法, 但是实在是,看不下去了,想了半个小时终于想出来一个,我觉得还算好的办法。请参看注释。


func pivotIndex(nums []int) int {

    length := len(nums)

    sumRight := 0

    sumLeft := 0

    sum := 0

    //count all the values to one

    for i := 0 ; i < length ; i ++ {

        sum += nums[i]

    }   

    //we have a sum for all the values counts, and then we use {right = (all-currentPos-left)}

    //if right == left , then we retrun the position.

    for pos := 0 ; pos < length ; pos++ {

        if pos != 0 {

            sumLeft += nums[pos-1]

        }

        sumRight = sum - nums[pos] - sumLeft

        if sumRight == sumLeft {

            return pos

        }

    }

    return -1

}

至少是其他数字两倍的最大数


func dominantIndex(nums []int) int {

    first , second := -1 , -1

    pos := -1

//Find the first and second bigger number in the array , if the first > 2*second, then we can return the position.

    for i := 0 ; i < len(nums) ; i++ {

        if first < nums[i] {

            second = first

            first = nums[i]

            pos = i

        } else if second < nums[i] {

            second = nums[i]

        }

    }

    if first >= second * 2{

        return pos

    }

    return -1

}

加一


func plusOne(digits []int) []int {

    length := len(digits)

    //do this just like we do the plus when we are young

    for i:= length -1  ; i >= 0 ; i-- {

        //if digits[i]<9 then we could just do digits[i]++

        if digits[i] < 9 {

            digits[i]++

            return digits

        }

        digits[i] = 0

    }

    res := []int{1}

    res = append(res , digits...)

    return res

}

对角线遍历

func findDiagonalOrder(matrix [][]int) []int {

//if the array is empty just return empty one.

    if len(matrix) == 0 {

        return []int{}

    }

    /*

    at the first , it will goes up right , then (row - 1) and (column + 1) ,if we meet the border then (row + 1) (column - 1)

    How to handle border.

    if go up right , when get the up but not right border , then go right (row + 0) and (cloumn + 1), if not then go down (row + 1)(column + 0)

    if go down left , when get the left but not down border , then go down (row + 1)(cloumn + 0) , if not then go right (row + 0)(cloumn + 1)

    */

    row , column := 0, 0

    m, n := len(matrix), len(matrix[0])

    ret := make([]int, m*n)

    for i := 0; i < m*n; i++ {

        ret[i] = matrix[row][column]

        if (row + column)%2 == 0 {

            if column == n-1 {

                row++

            } else if row == 0 {

                column++

            } else {

                row--

                column++

            }

        } else {

            if row == m-1 {

                column++

            } else if column == 0 {

                row++

            } else {

                row++

                column--

            }

        }

    }

    return ret

}

实现strStr()

func strStr(haystack string, needle string) int {
    //regarding the problem tips , we know when needle is nil we return 0; and if haystck == needle also return 0
    if len(needle) == 0 || haystack == needle {
        return 0
    }
    
    length := len(needle)
    //becareful array out of range. so we need len(haystack) - length
    for i := 0 ; i <= (len(haystack) - length) ;i++ {
        if haystack[i:i+length] == needle {
            return i
        }
    }
    
    return -1
}

最长公前缀

func longestCommonPrefix(strs []string) string {
    
    if len(strs) == 0{
        return ""
    }
    prefix := strs[0]
    length := len(strs)
    for i := 1; i < length; i++ {
        str := strs[i]
        for !strings.HasPrefix(str, prefix){
            if len(prefix) == 1 {
                prefix = ""
                break
            }
            prefix = prefix[0:len(prefix)-1]
        }
    }
    return prefix
}

字符串翻转

func reverseString(s []byte)  {
    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
        s[i], s[j] = s[j], s[i]
    }
}

数组拆分I

func arrayPairSum(nums []int) int {
    sort.Ints(nums)
    
    ret := 0
    //sort the array first then we put all odds togother.
    for i := 0; i < len(nums); i += 2 {
        ret += nums[i]
    }
    return ret
}

两数之和II

func twoSum(numbers []int, target int) []int {
    //start from left and right.
    
    left, right := 0, len(numbers)-1
    for left < right {
        if numbers[left]+numbers[right] < target {
            left++
        } else if numbers[left]+numbers[right] > target {
            right--
        } else {
            return []int{left + 1, right + 1}
        }
    }
    return nil
}

移除元素

func removeElement(nums []int, val int) int {
    if nums == nil {
        return 0
    }
    for i := 0; i < len(nums); {
        if nums[i] == val {
            nums = append(nums[:i], nums[i+1:]...)
            i--
        }
        i++
    }
    return len(nums)
}

最大连续1的个数

func findMaxConsecutiveOnes(nums []int) int {
    
    if (nums == nil) {
        return 0
    }
    
    max := 0
    temp := 0
    for i := 0 ;i < len(nums) ;i++ {
        if nums[i] == 1 {
            temp++
        } else {
            if temp > max {
                max = temp
            }
            temp = 0
        } 
    }
    //this part was stuck for more than 30 mins.
    //this part controls test case like [1] , [0,1] this two are so special
    if temp > max {
        max = temp
    }
    
    return max
}

杨辉三角形II

func getRow(rowIndex int) []int {
    ret := make([]int, rowIndex+1)
    for k := range ret {
        ret[k] = 1
    }
    for i := 0; i < rowIndex-1; i++ {
        for j := i + 1; j >= 1; j-- {
            ret[j] = ret[j] + ret[j-1]
        }
    }
    return ret
}

翻转字符串里的单词

func reverseWords(s string) string {
    arr := strings.Split(s, " ")
    res := []string{}
    for i := 0; i < len(arr); i++ {
        if arr[i] != "" {
            res = append(res, arr[i])
        }
    }
    for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
        res[i], res[j] = res[j], res[i]
    }

    return strings.Replace(strings.Trim(fmt.Sprint(res), "[]"), " ", " ", -1)
}

翻转单词III

func reverseWords(s string) string {
    sp := strings.Split(s, " ")
    ret := ""
    for i := 0; i < len(sp); i++ {
        t := make([]byte, len(sp[i]))
        for left, right := 0, len(sp[i])-1; left <= right; left, right = left+1, right-1 {
            t[left], t[right] = sp[i][right], sp[i][left]
        }
        ret += string(t)
        if i != len(sp)-1 {
            ret += " "
        }
    }
    return ret
}

移动零

func moveZeroes(nums []int)  {
    index := 0
    
    for i:= 0 ; i < len(nums) ; i++ {
        if(nums[i] != 0) {
            nums[index] = nums[i]
            index++
        }
    }
    
    for index < len(nums) {
        //diffrent from c/c++ you can't use nums[index++] here
        nums[index] = 0
        index++
    }
}

相关文章

  • Tourist with Data Structure Firs

    数组和字符串 寻找数组的中心索引. 本来使用暴力破解方法, 但是实在是,看不下去了,想了半个小时终于想出来一个,我...

  • Tourist with Data Structure Seco

    链表 读题要仔细,只看题干,容易死的很惨。 设计链表 环形链表 一般环形链表使用快慢指针方式去做,快慢指针算法。参...

  • Tourist with Data Structure Thir

    探索哈希表 概念 哈希集合:哈希集合是集合数据结构的实现之一,用于存储非重复值。哈希映射 :哈希映射是映射数据结构...

  • Tourist with Data Structure Fift

    设计循环列表 岛屿的数量 最小栈 有效的括号 每日温度 逆波兰表达式求值 克隆图 目标和 用栈实现队列 用栈实现队...

  • Tourist with Data Structure Fout

    递归 反转字符串 这个以前实现过, 这次试用递归的方式实现。 两两交换链表中的节点 不知道如何试用递归来完成。不过...

  • Data Structure

    stack heap 堆的基本性质:①堆中的某一个节点总是不小于或不大于其父节点的值。②堆总是一棵完全二叉树比较经...

  • Data Structure

    ds_using_list.py ds_using_tuple.py ds_using_dict.py ds_se...

  • Data structure

  • SICP-chapter2 Data Structure

    Introduction to Data Structure What is Meant by Data? The...

  • [Math] Persistent data structure

    In computing, a persistent data structure is a data struc...

网友评论

      本文标题:Tourist with Data Structure Firs

      本文链接:https://www.haomeiwen.com/subject/vrnxzqtx.html