-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutation.java
45 lines (37 loc) · 1.08 KB
/
Permutation.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
import edu.princeton.cs.algs4.StdIn;
public class Permutation {
public static void main(String[] args) {
if (args.length == 0) {
throw new IllegalArgumentException("Missing argument for number of items to display.");
}
int max = Integer.parseInt(args[0]);
RandomizedQueue<String> q = new RandomizedQueue<>();
StringBuilder word = new StringBuilder();
while (!StdIn.isEmpty()) {
char c = StdIn.readChar();
// StdOut.print(c);
if (c == '\n') {
q.enqueue(word.toString());
break;
}
if (c == ' ') {
// StdOut.print(word);
q.enqueue(word.toString());
word = new StringBuilder();
continue;
}
word.append(c);
}
//
// q.dequeue();
// q.dequeue();
// q.dequeue();
int i = 0;
for (String s: q) {
if (i++ == max) {
break;
}
System.out.printf("%s ", s);
}
}
}