0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【LeetCode】アルゴリズム体操 1920. Build Array from Permutation

Posted at

LeetCode - Build Array from Permutation

問題文

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.

A zero-based permutation nums is an array of distinct integers from 0 to nums.length - 1 (inclusive).

だいたいの日本語訳

引数:int[] nums
戻り値:int型の配列

与えられた配列(nums)と同じ要素数の配列(ans)を返してほしいんだけど、ans[i] = nums[nums[i]]みたいなかんじでお願い。
numsは0から配列の要素数-1までの数値がシャッフルされたものやで。

この問題は簡単すぎた

Solution.java
class Solution {
    public int[] buildArray(int[] nums) {
        int[] ans = new int[nums.length];
        
        for (int i = 0; i < nums.length; i++) {
            ans[i] = nums[nums[i]];
        }
        
        return ans;
    }
}

実行結果

Title RunTime Memory
この問題は簡単すぎた 1ms 40MB

ひとこと

己の心がやられないようにEasyから取り組んでますが、さすがに簡単すぎました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?