Re: [閒聊] 每日LeetCode已回收
: 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]
class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
res = []
for i in range(n) :
res.append(nums[i])
res.append(nums[i+n])
return res
我要用幼幼題參與這話題:))))))
可是怎麼先把nums分成
nums[:n]跟nums[n+1:]
在分別append進去可以比我那做法快一點
不是都是作n次嗎
:00000
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.81.131 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1675664604.A.ADA.html
→
02/06 15:08,
2年前
, 1F
02/06 15:08, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 219 之 719 篇):