首页 > 其他 > 详细

leetcode387

时间:2016-10-14 00:24:53      阅读:340      评论:0      收藏:0      [点我收藏+]

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.
找出第一次出现的不重复的字符
public class Solution {
    public int firstUniqChar(String s) {
        //97-122
        int[] map = new int[123];
        int i=0;
        int length = s.length();
        for(i=0;i<length;i++)
        {
            map[s.charAt(i)]++;
        }
        for(i=0;i<length;i++)
        {
            if(map[s.charAt(i)] == 1)
                return i;
        }
        return -1;
    }
}

除了两次for循环,暂时没有想到N时间复杂度的解法。

leetcode387

原文:http://www.cnblogs.com/linkstar/p/5958600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!