-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain8.java
More file actions
53 lines (48 loc) · 1.58 KB
/
Main8.java
File metadata and controls
53 lines (48 loc) · 1.58 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Created by LXF on 2017/8/15.
*/
public class Main8 {
public static int FirstNotRepeatingChar(String str) {
int n = str.length();
for (int i = 0; i < n; i++) {
String s2 = str.substring(i + 1,n);
String s1 = "";
if (i != 0) {
s1 = str.substring(0, i);
}
if (!s2.contains(str.charAt(i) + "")&&!s1.contains(str.charAt(i) + "")) {
return i;
}
}
return 0;
}
public static int FirstNotRepeatingChar1(String str) {
if(str == null || str.length()==0) return -1;
List<Character> list = new ArrayList<>();
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if(!list.contains(ch)){
list.add(Character.valueOf(ch));
}else{
list.remove(Character.valueOf(ch));
}
}
if(list.size() <=0) return -1;
return str.indexOf(list.get(0));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
Long start = System.currentTimeMillis();
System.out.println(FirstNotRepeatingChar(s));
Long end = System.currentTimeMillis();
System.out.println(end-start);
Long start1 = System.currentTimeMillis();
System.out.println(FirstNotRepeatingChar1(s));
Long end1 = System.currentTimeMillis();
System.out.println(end1-start1);
}
}