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

xiaoxiao2021-02-27  215

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'); } }
转载请注明原文地址: https://www.6miu.com/read-11572.html

最新回复(0)