-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslate.java
More file actions
96 lines (77 loc) · 1.7 KB
/
Translate.java
File metadata and controls
96 lines (77 loc) · 1.7 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
public class Translate {
public static void main(String[] args){
String ss = "AAAAAAAAAAAAAAAAAAAAAAAABCCCC";
char[] dest = new char[10];
encode(ss.toCharArray(), dest);
System.out.println(Arrays.toString(dest));
}
public static void encode(char[] text, char[] dest){
char a = text[0];
int sa = 1;
LinkedList<Character> records = new LinkedList<Character>();
for(int i = 1; i<text.length; i++){
if (text[i] != a){
records.add(new Character(a));
records.add(new Character((char)sa));
a = text[i];
sa = 1;
}
else{
sa ++;
}
}
records.add(new Character(a));
records.add(new Character((char)sa));
Iterator<Character> I = records.iterator();
int in = 0;
String str = new String();
while(I.hasNext()){
String aa = ""+ I.next().charValue();
int saa = (int)I.next().charValue();
if (aa.equals("1")){
aa = "1" + aa;
}
if(saa == 1){
if (in == 0){
str = str + '1'+ aa;
in = 1;
}else if (in == 1){
str = str + aa;
}
}
else if(saa>=2&&saa<=9){
if (in == 0){
str = str + saa + aa;
}else if (in == 1){
str = str + '1' + saa + aa;
in = 0;
}
}
else if(saa > 9){
if (in == 0){
while(saa > 9){
str = str + "9" + aa;
saa = saa - 9;
}
str = str + saa + aa;
}else if (in == 1){
str = str + '1';
in = 0;
while(saa > 9){
str = str + "9" + aa;
saa = saa - 9;
}
str = str + saa + aa;
}
}
}
if (in == 1){
str = str + '1';
}
System.out.print(str);
dest = str.toCharArray();
}
}