您的位置:首页 > Web前端

剑指offer——第一个只出现一次的字符

2017-07-10 07:41 197 查看
public class Solution {
public int FirstNotRepeatingChar(String str) {

if(str == null) return -1;
if(str.length() == 0) return -1;
int[] pos = new int[100];

for(int i = 0; i < 100; ++i)
pos[i] = -2;

for(int i = 0; i < str.length(); ++i) {
if(pos[str.charAt(i) - 65] == -2) pos[str.charAt(i) - 65] = i;
else {
pos[str.charAt(i) - 65] = -1;
}
}

for(int i = 0; i < str.length(); ++i) {
if(pos[str.charAt(i) - 65] != -2 && pos[str.charAt(i) - 65] != -1)
return pos[str.charAt(i) - 65];
}
return 0;
}

public static void main(String[] args) {
System.out.println((int)'z');
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: