Sort Array By Parity
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
JAVA
class Solution {
public int[] sortArrayByParity(int[] A) {
int[] arr = new int[A.length];
int begin = 0;
int end = A.length - 1;
for(int i = 0;i < A.length;i++){
//[3,1,2,4]
if(A[i] % 2 == 0){
arr[begin++] = A[i];
}else{
arr[end--] = A[i];
}
}
return arr;
}
}
Success
Details
Runtime: 1 ms, faster than 100.00% of Java online submissions for Sort Array By Parity.
Memory Usage: 38.8 MB, less than 95.18% of Java online submissions for Sort Array By Parity.
python3
class Solution:
def sortArrayByParity(self, A: List[int]) -> List[int]:
current = 0
for i, el in enumerate(A):
if not el % 2:
A[current], A[i] = A[i], A[current]
current = current + 1
return A
Success
Details
Runtime: 68 ms, faster than 86.73% of Python3 online submissions for Sort Array By Parity.
Memory Usage: 13.6 MB, less than 5.69% of Python3 online submissions for Sort Array By Parity.
python里面的enumerate函数输出的分别是索引及数值,很好用。
class Solution:
def sortArrayByParity(self, A: List[int]) -> List[int]:
c = 0
for i, j in enumerate(A):
if j % 2==0:
A[c], A[i] = A[i], A[c]
c += 1
return A
X of a Kind in a Deck of Cards
In a deck of cards, each card has an integer written on it.
Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where:
Each group has exactly X cards.
All the cards in each group have the same integer.
Example 1:
Input: [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4]
Example 2:
Input: [1,1,1,2,2,2,3,3]
Output: false
Explanation: No possible partition.
Example 3:
Input: [1]
Output: false
Explanation: No possible partition.
Example 4:
Input: [1,1]
Output: true
Explanation: Possible partition [1,1]
Example 5:
Input: [1,1,2,2,2,2]
Output: true
Explanation: Possible partition [1,1],[2,2],[2,2]
Note:
1 <= deck.length <= 10000
0 <= deck[i] < 10000
GCD方法:
JAVA
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] bucket = new int[10001];
for (int card : deck)
bucket[card]++;
int min = 10001;
for (int val : bucket)
if (val != 0) min = java.lang.Math.min(min, val);
if (min == 1)
return false;
for (int val : bucket)
if (hcf(min, val) == 1) return false;
return true;
}
private int hcf(int a, int b){
int hcf = 1;
for(int i = 1; i <= a || i <= b; i++) {
if( a%i == 0 && b%i == 0 )
hcf = i;
}
return hcf;
}
}
Success
Details
Runtime: 9 ms, faster than 71.19% of Java online submissions for X of a Kind in a Deck of Cards.
Memory Usage: 38.7 MB, less than 82.41% of Java online submissions for X of a Kind in a Deck of Cards.
python3
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
count = collections.Counter(deck)
mini = min(count.values())
if mini < 2:
return False
for i in range(mini+1,1,-1):
res = all(value % i ==0 for value in count.values())
if res: return True
return False
Success
Details
Runtime: 40 ms, faster than 99.88% of Python3 online submissions for X of a Kind in a Deck of Cards.
Memory Usage: 13.5 MB, less than 6.12% of Python3 online submissions for X of a Kind in a Deck of Cards.
Sort Array By Parity II
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.
Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
You may return any answer array that satisfies this condition.
Example 1:
Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Note:
2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
JAVA
class Solution {
public int[] sortArrayByParityII(int[] A) {
int i = 0, j = 1, n = A.length;
while (i < n && j < n) {
while (i < n && A[i] % 2 == 0) {
i += 2;
}
while (j < n && A[j] % 2 == 1) {
j += 2;
}
if (i < n && j < n) {
swap(A, i, j);
}
}
return A;
}
private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
Success
Details
Runtime: 2 ms, faster than 99.75% of Java online submissions for Sort Array By Parity II.
Memory Usage: 39.6 MB, less than 99.35% of Java online submissions for Sort Array By Parity II.
python3
class Solution:
def sortArrayByParityII(self, A: List[int]) -> List[int]:
even, odd = [a for a in A if not a % 2], [a for a in A if a % 2]
return [even.pop() if not i % 2 else odd.pop() for i in range(len(A))]
Success
Details
Runtime: 140 ms, faster than 78.40% of Python3 online submissions for Sort Array By Parity II.
Memory Usage: 15.4 MB, less than 5.49% of Python3 online submissions for Sort Array By Parity II.
def re_odd_err(A,i):
for n in range(i,len(A)):
if n%2==0 and A[n]%2!=0:
return n
def re_eve_err(A,i):
for n in range(i,len(A)):
if n%2!=0 and A[n]%2==0:
return n
class Solution:
def sortArrayByParityII(self, A: List[int]) -> List[int]:
i=0
n=0
m=0
while(i<len(A)):
if i%2==0 and A[i]%2!=0:
n=re_eve_err(A,i)
A[i],A[n]=A[n],A[i]
if n<i:
i=n
elif i%2!=0 and A[i]%2==0:
m=re_odd_err(A,i)
A[i],A[m]=A[m],A[i]
if m<i:
i=m
i=i+1
return A
SuccessDetails Runtime: 436 ms, faster than 5.10% of Python3 online submissions for Sort Array By Parity II.Memory Usage: 16 MB, less than 8.70% of Python3 online submissions for Sort Array By Parity II.