★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-widigbdk-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two integers n
and k
, you need to construct a list which contains n
different positive integers ranging from 1
to n
and obeys the following requirement:
Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k
distinct integers.git
If there are multiple answers, print any of them.github
Example 1:數組
Input: n = 3, k = 1 Output: [1, 2, 3] Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.
Example 2:微信
Input: n = 3, k = 2 Output: [1, 3, 2] Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.
Note:app
n
and k
are in the range 1 <= k < n <= 10^4.給定兩個整數 n
和 k
,你須要實現一個數組,這個數組包含從 1
到 n
的 n
個不一樣整數,同時知足如下條件:ui
① 若是這個數組是 [a1, a2, a3, ... , an] ,那麼數組 [|a1 - a2|, |a2- a3|, |a3 - a4|, ... , |an-1 - an|] 中應該有且僅有 k 個不一樣整數;.this
② 若是存在多種答案,你只需實現並返回其中任意一種.spa
示例 1:code
輸入: n = 3, k = 1 輸出: [1, 2, 3] 解釋: [1, 2, 3] 包含 3 個範圍在 1-3 的不一樣整數, 而且 [1, 1] 中有且僅有 1 個不一樣整數 : 1
示例 2:
輸入: n = 3, k = 2 輸出: [1, 3, 2] 解釋: [1, 3, 2] 包含 3 個範圍在 1-3 的不一樣整數, 而且 [2, 1] 中有且僅有 2 個不一樣整數: 1 和 2
提示:
n
和 k
知足條件 1 <= k < n <= 10^4.1 class Solution { 2 func constructArray(_ n: Int, _ k: Int) -> [Int] { 3 var k = k 4 var res:[Int] = [Int]() 5 var i:Int = 1 6 var j:Int = n 7 while (i <= j) 8 { 9 var num:Int = 0 10 if k > 1 11 { 12 if k % 2 != 0 13 { 14 num = i 15 i += 1 16 } 17 else 18 { 19 num = j 20 j -= 1 21 } 22 k -= 1 23 } 24 else 25 { 26 num = i 27 i += 1 28 } 29 res.append(num) 30 } 31 return res 32 } 33 }
36ms
1 class Solution { 2 func constructArray(_ n: Int, _ k: Int) -> [Int] { 3 var res = Array(repeating: 0, count: n) 4 var l = 1, r = n 5 for i in 0..<k { 6 if i % 2 == 0 { 7 res[i] = l 8 l += 1 9 } else { 10 res[i] = r 11 r -= 1 12 } 13 } 14 if k % 2 == 1 { 15 for i in k..<n { 16 res[i] = l 17 l += 1 18 } 19 } else { 20 for i in k..<n { 21 res[i] = r 22 r -= 1 23 } 24 } 25 26 return res 27 } 28 }
44ms
1 class Solution { 2 func constructArray(_ n: Int, _ k: Int) -> [Int] { 3 if k == 1 { return Array(1 ... n) } 4 5 func gen(_ k: Int) -> [Int] { 6 var ret = Array(repeating: 0, count: k) 7 for i in ret.indices { 8 ret[i] = i % 2 == 0 ? i/2 + 1 : k - i/2 9 } 10 return ret 11 } 12 13 if k + 1 == n { return gen(n) } 14 return gen(k + 1) + Array(k + 2 ... n) 15 } 16 }
60ms
1 class Solution { 2 func constructArray(_ n: Int, _ k: Int) -> [Int] { 3 var result = [Int](repeating: 0, count: n) 4 5 var j = 0 6 for i in 0..<(n - k - 1) { 7 result[i] = i + 1 8 j += 1 9 } 10 11 for i in 0...k { 12 result[j] = i % 2 == 0 13 ? n - k + i / 2 14 : n - i / 2 15 j += 1 16 } 17 18 return result 19 } 20 }