美文网首页
Tourist with Data Structure Fout

Tourist with Data Structure Fout

作者: Jiawei_84a5 | 来源:发表于2019-06-09 18:22 被阅读0次

递归

反转字符串

这个以前实现过, 这次试用递归的方式实现。

func reverseString(s []byte)  {
    helper(0 , len(s) -1 , s)
}

func helper(start int , end int , s []byte) {
    if end < start {
        return
    }
    
    s[start] , s[end] = s[end] ,s[start]
    
    helper(start+1 , end-1 , s)
}

两两交换链表中的节点

不知道如何试用递归来完成。不过会在以后的补充。

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func swapPairs(head *ListNode) *ListNode {
    if head == nil {
        return head
    }
    
    current := head
    next := head.Next
    
    for current != nil && next != nil {
        current.Val , next.Val = next.Val , current.Val
        
        current = next.Next
        
        if current == nil || next.Next == nil {
            break
        } else {
            next = current.Next
        }
    }
    
    return head
}

斐波那契数列

func fib(N int) int {
    if N == 0 {
        return 0
    }
    if N == 1 {
        return 1
    }
    return fib(N-1) + fib(N-2)
}

爬楼梯

func climbStairs(n int) int {
    if n < 2 {
        return 1
    }
    tmp := []int{1, 2}
    for i := 2; i < n; i++ {
        tmp = append(tmp, tmp[i-1]+tmp[i-2])
    }
    return tmp[n-1]
}

二叉树的最大深度

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    l := maxDepth(root.Left)
    r := maxDepth(root.Right)
    return max(l, r) + 1
}
func max(x, y int) int {
    if x > y {
        return x
    }
    return y
}

Pow()

func myPow(x float64, n int) float64 {
    if x == 0 {
        return 0
    }
    result := calPow(x, n)
    if n < 0 {
        result = 1 / result
    }
    return result
}
 
func calPow(x float64, n int) float64 {
    if n == 0 {
        return 1
    }
    if n == 1 {
        return x
    }
 
    // 向右移动一位
    result := calPow(x, n>>1)
    result *= result
 
    // 如果n是奇数
    if n&1 == 1 {
        result *= x
    }
 
    return result
}

第K个语法符号

用C++ 更简单

class Solution {
public:
    int kthGrammar(int N, int K) {
        int res = 0;
        while (K > 1) {
            K = (K % 2 == 1) ? K + 1 : K / 2;
            res ^= 1;
        }
        return res;
    }
};

不同的二叉搜索树 II

class Solution {
public:
    vector<TreeNode *> generateTrees(int n) {
        if (n == 0) return {};
        return *generateTreesDFS(1, n);
    }
    vector<TreeNode*> *generateTreesDFS(int start, int end) {
        vector<TreeNode*> *subTree = new vector<TreeNode*>();
        if (start > end) subTree->push_back(NULL);
        else {
            for (int i = start; i <= end; ++i) {
                vector<TreeNode*> *leftSubTree = generateTreesDFS(start, i - 1);
                vector<TreeNode*> *rightSubTree = generateTreesDFS(i + 1, end);
                for (int j = 0; j < leftSubTree->size(); ++j) {
                    for (int k = 0; k < rightSubTree->size(); ++k) {
                        TreeNode *node = new TreeNode(i);
                        node->left = (*leftSubTree)[j];
                        node->right = (*rightSubTree)[k];
                        subTree->push_back(node);
                    }
                }
            }
        }
        return subTree;
    }
};

相关文章

  • Tourist with Data Structure Fout

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

  • Tourist with Data Structure Firs

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

  • Tourist with Data Structure Seco

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

  • Tourist with Data Structure Thir

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

  • Tourist with Data Structure Fift

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

  • 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 Fout

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