LeetCode 387. First Unique Character in a String

本文详细介绍了如何通过不同Java实现方式解决LeetCode上的一道题目——寻找字符串中第一个只出现一次的字符,并对其索引进行返回。文章探讨了使用数组、LinkedHashMap以及HashSet等数据结构来提高查找效率的方法。

Problem:

 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 letters. 

 

try:

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

 result:

想当然!

second:

import java.util.HashMap;
import java.util.LinkedHashMap;

class Solution {
    public int firstUniqChar(String s) {
        HashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
        for(int i=0;i<s.length();i++){
            //if(map.get((Character)s.charAt(i))==null) map.put()
            Character c = s.charAt(i);
            Integer int = map.get(c);
            map.put(c, integer==null ? 1 : (++integer)); 
        }
        
        for(MapEntry entry : map){
            if(entry.getValue()==1) return entry.getKey();
        }
        
        return -1;
    }
}

 

result:

Run Code Status: Compile Error
Run Code Result:
Your input

"leetcode"

Your answer

Line 10: error: not a statement

Expected answer

0

 

 

被这个stupid的问题卡了好久。。因为leetcode的编辑器没有数据类型的关键字高亮提示,所以把这个问题忽略了,int是不能作为变量名称使用的。

 

re-try:

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

class Solution {
    public int firstUniqChar(String s) {
        HashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
        for(int i=0;i<s.length();i++){
            //if(map.get((Character)s.charAt(i))==null) map.put()
            Character c = s.charAt(i);
            Integer integer = map.get(c);
            map.put(c, integer==null ? 1 : (++integer)); 
        }
        
        for(Map.Entry<Character, Integer> entry : map.entrySet()){
            if(entry.getValue()==1){
                return s.indexOf(entry.getKey());    
            } 
        }
        
        
        
        return -1;
    }
}

 

result:

 分析:

用LinkedHashMap效率较低,应该可以找到更优方案。

re-try:

import java.util.HashSet;

class Solution {
    public int firstUniqChar(String s) {
        int[] counts = new int[26];
        char[] charArray = s.toCharArray();
        for(char c : charArray){
            counts[c-'a']++;
        }
        
        HashSet<Character> set = new HashSet<Character>();
        for(int i=0;i<counts.length;i++){
            if(counts[i]==1) set.add((char)('a'+i));
        }
        
        for(int i=0;i<charArray.length;i++){
            if(set.contains(charArray[i])) return i;
        }
        
        return -1;
    }
}

 

result:

 

conclusion:

 

转载于:https://www.cnblogs.com/hzg1981/p/8955391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值