Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-29 #344

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-29

jingkecn commented 2 years ago
/*
 * @lc app=leetcode.cn id=1470 lang=csharp
 *
 * [1470] 重新排列数组
 */

// @lc code=start
public class Solution {
    public int[] Shuffle(int[] nums, int n) {
        var result = new int[2 * n];
        for (var i = 0; i < n; i++) {
            result[2 * i] = nums[i];
            result[2 * i + 1] = nums[i + n];
        }

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

微信id: 我要成為 Dr. 🥬 来自 vscode 插件

gongpeione commented 2 years ago
/*
 * @lc app=leetcode id=235 lang=typescript
 *
 * [235] Lowest Common Ancestor of a Binary Search Tree
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
    let cur = root;

    while (cur) {
        // if the val of two nodes both greater than current node
        // then these two nodes are both from the right sub tree
        if (p.val > cur.val && q.val > cur.val) {
            cur = cur.right;
        } else if (p.val < cur.val && q.val < cur.val) {
            cur = cur.left;
        } else {
            return cur;
        }
    }

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

微信id: 弘树 来自 vscode 插件

dreamhunter2333 commented 2 years ago
#include <iostream>
#include <vector>
using namespace std;
/*
 * @lc app=leetcode.cn id=1470 lang=cpp
 *
 * [1470] 重新排列数组
 */

// @lc code=start
class Solution
{
public:
    vector<int> shuffle(vector<int> &nums, int n)
    {
        vector<int> res;
        for (size_t i = 0; i < n; i++)
        {
            res.push_back(nums[i]);
            res.push_back(nums[i + n]);
        }
        return res;
    }
};
// @lc code=end

微信id: 而我撑伞 来自 vscode 插件

SaraadKun commented 2 years ago

1470. 重新排列数组

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int[] ans = new int[2 * n];
        for (int i = 0, j = n, idx = 0; i < n; i++, j++) {
            ans[idx++] = nums[i];
            ans[idx++] = nums[j];
        }
        return ans;
    }
}

WeChat: Saraad