-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJz34.java
More file actions
41 lines (36 loc) · 908 Bytes
/
Jz34.java
File metadata and controls
41 lines (36 loc) · 908 Bytes
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
package com.cpucode.java.simple;
/**
* 题目描述
* 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置,
* 如果没有则返回 -1(需要区分大小写).(从0开始计数)
*
* 示例1
* 输入
* "google"
* 返回值
* 4
*
* @author : cpucode
* @Date : 2021/1/23
* @Time : 11:48
* @Github : https://github.com/CPU-Code
* @CSDN : https://blog.csdn.net/qq_44226094
*/
public class Jz34 {
public int FirstNotRepeatingChar(String str) {
if(str == null){
return -1;
}
int[] count = new int[128];
char[] strs = str.toCharArray();
for(char s : strs){
count[s]++;
}
for(int i = 0; i < strs.length; i++){
if(count[strs[i]] == 1){
return i;
}
}
return -1;
}
}