Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-03 #318

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-03

restartgr commented 2 years ago
/*
 * @lc app=leetcode.cn id=106 lang=javascript
 *
 * [106] 从中序与后序遍历序列构造二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} inorder
 * @param {number[]} postorder
 * @return {TreeNode}
 */
var buildTree = function(inorder, postorder) {
    if (!inorder.length) return null;
    const rootVal = postorder.pop(); // 从后序遍历的数组中获取中间节点的值, 即数组最后一个值
    let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
    const root = new TreeNode(rootVal); // 创建中间节点
    root.left = buildTree(inorder.slice(0, rootIndex), postorder.slice(0, rootIndex)); // 创建左节点
    root.right = buildTree(inorder.slice(rootIndex + 1), postorder.slice(rootIndex)); // 创建右节点
    return root;
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

restartgr commented 2 years ago
/*
 * @lc app=leetcode.cn id=105 lang=javascript
 *
 * [105] 从前序与中序遍历序列构造二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
var buildTree = function (preorder, inorder) {
    if (!preorder.length) return null;
    const rootVal = preorder.shift(); // 从前序遍历的数组中获取中间节点的值, 即数组第一个值
    let rootIndex = inorder.indexOf(rootVal); // 获取中间节点在中序遍历中的下标
    const root = new TreeNode(rootVal); // 创建中间节点
    root.left = buildTree(preorder.slice(0, rootIndex), inorder.slice(0, rootIndex)); // 创建左节点
    root.right = buildTree(preorder.slice(rootIndex), inorder.slice(rootIndex + 1)); // 创建右节点
    return root;
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=206 lang=typescript
 *
 * [206] Reverse Linked List
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function reverseList(head: ListNode | null): ListNode | null {
    let headPointer = head;
    let prev = null;

    while(headPointer) {
        // store the next node
        const next = headPointer.next;
        // point the next node of current head node to prev node
        headPointer.next = prev;
        // current head node become the prev node
        prev = headPointer;
        // next node become the head node
        headPointer = next;
    }

    return prev;
};
// @lc code=end

微信id: 弘树 来自 vscode 插件

restartgr commented 2 years ago
/*
 * @lc app=leetcode.cn id=654 lang=javascript
 *
 * [654] 最大二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var constructMaximumBinaryTree = function (nums) {
    const buildTree = (arr, left, right) => {
        if (left > right) {
            return null
        }
        let max = -1
        let index = -1
        for (let i = left; i <= right; i++) {
            if (max < arr[i]) {
                index = i
                max = arr[i]
            }
        }
        const node = new TreeNode(max)
        node.left = buildTree(arr, left, index - 1)
        node.right = buildTree(arr, index + 1, right)
        return node
    }
    return buildTree(nums, 0, nums.length - 1)
};
// @lc code=end

微信id: Jörmungandr 来自 vscode 插件

SaraadKun commented 2 years ago

899. 有序队列

class Solution {
    public String orderlyQueue(String s, int k) {
        char[] chs = s.toCharArray();
        if (k > 1) {
            Arrays.sort(chs);
            return new String(chs);
        }
        int idx = 0, n = chs.length;
        char min = chs[idx];
        for (int i = 1; i < n; i++) {
            if (chs[i] < min) {
                min = chs[i];
                idx = i;
            } else if (chs[i] == min) {
                if (check(i, idx, n, chs)) {
                    idx = i;
                }
            }
        }
        return s.substring(idx) + s.substring(0, idx);
    }

    private boolean check(int hi, int lo, int n, char[] chs) {
        while (lo < hi && chs[hi] == chs[lo]) {
            hi = hi == n - 1 ? 0 : hi + 1;
            lo++;
        }
        return hi < n && chs[hi] < chs[lo];
    }
}

WeChat:Saraad

SaraadKun commented 2 years ago

剑指 Offer II 030. 插入、删除和随机访问都是 O(1) 的容器

class RandomizedSet {

    //最多进行 2 * 105 次 insert , remove 和 getRandom 方法调用
    int[] ids = new int[200010];
    Map<Integer, Integer> map = new HashMap<>();
    int p = 0;
    Random rd = new Random();
    /** Initialize your data structure here. */
    public RandomizedSet() {

    }

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if (map.containsKey(val)) {
            return false;
        }
        ids[p] = val;
        map.put(val, p++);
        return true;
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if (!map.containsKey(val)) {
            return false;
        }
        int idx = map.get(val);
        ids[idx] = ids[--p];
        map.put(ids[idx], idx);
        map.remove(val);
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        return ids[rd.nextInt(p)];
    }
}

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * RandomizedSet obj = new RandomizedSet();
 * boolean param_1 = obj.insert(val);
 * boolean param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */

WeChat:Saraad