First Position Unique Character

来源:互联网 发布:域名紧急升级 编辑:程序博客网 时间:2024/05/19 03:18
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
样例
Given s = "lintcode", return 0.

Given s = "lovelintcode", return 2.

import java.util.Scanner;/** * Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.样例Given s = "lintcode", return 0.Given s = "lovelintcode", return 2. *  * @author Dell * */public class Test646 {  public static int firstUniqChar(String s)  {     if(s.equals(""))     {     return 0;     }  int[] chr=new int[256];  for(int i=0;i<s.length();i++)  { chr[s.charAt(i)]++;  } for(int i=0;i<s.length();i++) { if(chr[s.charAt(i)]==1) { return i; } } return -1;  }public static void main(String[] args) {Scanner sc=new Scanner(System.in);String s=sc.next();System.out.println(firstUniqChar(s));}}


原创粉丝点击