Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間2年前 (2023/02/06 13:35), 2年前編輯推噓2(201)
留言3則, 3人參與, 2年前最新討論串218/719 (看更多)
1470. Shuffle the Array 給你一個陣列和一個數字n,陣列有2n個元素,若陣列為:[x1,x2,...,xn,y1,y2,...,yn] ,返回如右形式的陣列:[x1,y1,x2,y2,...,xn,yn]。 Example: Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7] Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. Input: nums = [1,2,3,4,4,3,2,1], n = 4 Output: [1,4,2,3,3,2,4,1] 思路: 1.按照題目的要求把元素一個一個放進陣列就好 JavaCode: ------------------------------------------ class Solution { public int[] shuffle(int[] nums, int n) { int[] res = new int[nums.length]; int idx = 0; for (int i = 0; i < n; i++) { for (int j = i; j < nums.length; j += n) { res[idx++] = nums[j]; } } return res; } } ------------------------------------------ -- https://i.imgur.com/tdaniED.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.92.2 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1675661715.A.3E9.html ※ 編輯: Rushia (1.160.92.2 臺灣), 02/06/2023 13:36:36

02/06 13:40, 2年前 , 1F
大師
02/06 13:40, 1F

02/06 13:40, 2年前 , 2F
大師
02/06 13:40, 2F

02/06 14:36, 2年前 , 3F
大師
02/06 14:36, 3F
文章代碼(AID): #1Zu96JFf (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1Zu96JFf (Marginalman)