Java [Leetcode 387]First Unique Character in a String

题目描述:

Given a string, find the first non-repeating character in it and return it‘s index. If it doesn‘t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

解题思路:

开个26个数的数组,然后先对字符串过一遍,统计每个字母出现的次数,然后从头再国一遍,第一个字母数为1的即为首先出现并且只出现一次的字母。

代码如下:

public class Solution {
    public int firstUniqChar(String s) {
        int[] a = new int[26];
        for(int i = 0; i < s.length(); i++)
        	a[s.charAt(i) - ‘a‘]++;
        for(int i = 0; i < s.length(); i++){
        	if(a[s.charAt(i) - ‘a‘] == 1)
        		return i;
        }
        return -1;
    }
}

  

时间: 2024-10-10 17:38:35

Java [Leetcode 387]First Unique Character in a String的相关文章

[LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

[leetcode]387. First Unique Character in a String第一个不重复字母

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

leetcode修炼之路——387. First Unique Character in a String

最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: Y

387. First Unique Character in a String

https://leetcode.com/problems/first-unique-character-in-a-string/#/description Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "lovel

【LeetCode】387. First Unique Character in a String

Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-character-in-a-string/ Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s =

[LeetCode] NO. 387 First Unique Character in a String

[题目] Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowe

LeetCode之387. First Unique Character in a String

-------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solution { public int firstUniqChar(String s) { Count c[]=new Count[26]; for(int i=0;i<s.length();i++){ int index=s.charAt(i)-'a'; if(c[index]==null) c[in

[Algorithm] 387. First Unique Character in a String

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

LeetCode(387)First Unique Character in a String

题目 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowerc