[Swift Weekly Contest 114]LeetCode954. 二倍数对数组 | Array of Doubled Pairs

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.

Example 1:

Input: [3,1,3,6]
Output: false

Example 2:

Input: [2,1,2,6]
Output: false

Example 3:

Input: [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Example 4:

Input: [1,2,4,16,8,4]
Output: false

Note:

  1. 0 <= A.length <= 30000
  2. A.length is even
  3. -100000 <= A[i] <= 100000


给定一个长度为偶数的整数数组 A,只有对 A 进行重组后可以满足 “对于每个 0 <= i < len(A) / 2,都有 A[2 * i + 1] = 2 * A[2 * i]” 时,返回 true;否则,返回 false

示例 1:

输入:[3,1,3,6]
输出:false

示例 2:

输入:[2,1,2,6]
输出:false

示例 3:

输入:[4,-2,2,-4]
输出:true
解释:我们可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4]

示例 4:

输入:[1,2,4,16,8,4]
输出:false

提示:

  1. 0 <= A.length <= 30000
  2. A.length 为偶数
  3. -100000 <= A[i] <= 100000

1100ms

 1 class Solution {
 2     func canReorderDoubled(_ A: [Int]) -> Bool {
 3         var n:Int = A.count
 4         var a:[Int] = [Int](repeating:0,count:n)
 5         for i in 0..<n
 6         {
 7             if A[i] < 0
 8             {
 9                 a[i] = -A[i]*100000000
10             }
11             else
12             {
13                 a[i] = A[i]
14             }
15         }
16         a = a.sorted(by: <)
17         var p:Int = 0
18         var done:[Bool] = [Bool](repeating:false,count:n)
19         for i in 0..<n
20         {
21             if !done[i]
22             {
23                 done[i] = true
24                 while(p < n && (a[p] != a[i] * 2 || done[p]))
25                 {
26                     p += 1
27                 }
28                 if p == n {return false}
29                 done[p] = true
30             }
31         }
32         return true
33     }
34 }

原文地址:https://www.cnblogs.com/strengthen/p/10090740.html

时间: 2024-08-30 00:02:43

[Swift Weekly Contest 114]LeetCode954. 二倍数对数组 | Array of Doubled Pairs的相关文章

[Swift Weekly Contest 114]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order 

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 112]LeetCode947. 移除最多的同行或同列石头 | Most Stones Removed with Same Row or Column

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone. Now, a move consists of removing a stone that shares a column or row with another stone on the grid. What is the largest possible num

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 120]LeetCode980. 不同路径 III | Unique Paths III

On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is exactly one starting square. 2 represents the ending square.  There is exactly one ending square. 0 represents empty squares we can walk over. -1 repre

[Swift Weekly Contest 124]LeetCode993. 二叉树的堂兄弟节点 | Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique valu

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <