-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRevealCardsInIncreasingOrder.java
59 lines (56 loc) · 1.76 KB
/
RevealCardsInIncreasingOrder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*https://leetcode.com/problems/reveal-cards-in-increasing-order/*/
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
if (deck.length <= 2) return deck;
Arrays.sort(deck);
int N = deck.length;
ArrayList<Integer> resultList = new ArrayList<Integer>();
resultList.add(deck[N-2]);
resultList.add(deck[N-1]);
// System.out.println(resultList);
for (int i = N-3; i >= 0; --i)
{
int move = resultList.get(resultList.size()-1);
resultList.remove(resultList.size()-1);
resultList.add(0,move);
resultList.add(0,deck[i]);
// System.out.println(resultList);
}
int[] resultArr = convert(resultList);
return resultArr;
}
private int[] convert(ArrayList<Integer> list)
{
int size = list.size();
int[] arr = new int[size];
for (int i = 0; i < size; ++i)
arr[i] = (Integer)list.get(i);
return arr;
}
}
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
if (deck.length <= 2) return deck;
Arrays.sort(deck);
int N = deck.length;
Deque<Integer> deque = new LinkedList<Integer>();
deque.addLast(deck[N-2]);
deque.addLast(deck[N-1]);
for (int i = N-3; i >= 0; --i)
{
int move = deque.pollLast();
deque.addFirst(move);
deque.addFirst(deck[i]);
}
int[] resultArr = convert(deque);
return resultArr;
}
private int[] convert(Deque<Integer> list)
{
int size = list.size();
int[] arr = new int[size];
for (int i = 0; i < size; ++i)
arr[i] = (Integer)list.pollFirst();
return arr;
}
}