-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstAppearChara35.java
More file actions
49 lines (44 loc) · 1.41 KB
/
FirstAppearChara35.java
File metadata and controls
49 lines (44 loc) · 1.41 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
package offer;
/*
* 第一次只出现一次的字符 的位置;
*/
public class FirstAppearChara35 {
public static String chinaToUnicode(String str){
String result="";
for (int i = 0; i < str.length(); i++){
int chr1 = (char) str.charAt(i);
if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)
result+="\\u" + Integer.toHexString(chr1);
}else{
result+=str.charAt(i);
}
}
return result;
}
public int FirstNotRepeatingChar(String str) {
if(str.length() == 0) return -1;
int [] hashTable = new int[256];
for (int i = 0; i < str.length(); i++) {
hashTable[(int)str.charAt(i)] += 1;
}
/*
* 可能有多个出现次数 是 1
*/
for (int i = 0; i < str.length(); i++) {
if(hashTable[(int)str.charAt(i)] == 1){
return i;
}
}
return -1;
}
public static void main(String[] args) {
String str = "aabb";
System.out.println(new FirstAppearChara35().FirstNotRepeatingChar(str));
String[] chara = {"我","是"};
String unicode = "";
for (int i = 0; i < chara.length; i++) {
unicode += chinaToUnicode(chara[i]);
}
System.out.println( unicode );
}
}