LeetCode387. First Unique Character in a String

原创 2016年08月28日 23:40:55

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.

class Solution {
public:
    int firstUniqChar(string s) {
        int num[26] = {0};
        int pos[26] = {0};
        for (int i = 0; i < s.size(); i++) {
            char c = s[i];
            num[c-'a']++;
            pos[c-'a'] = i;
        }
        int i = 0;
        int idx = s.size();
        while (i < 26)
        {
            if (num[i] == 1) {
                if (pos[i] < idx) idx = pos[i];
            }
            i++;
        }
        if (idx == s.size())
            idx = -1;
        return idx;
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode之路:387. First Unique Character in a String

LeetCode之路:387. First Unique Character in a String 一、引言这道题题意非常简单,却让我思考了很久,或许难的并不是这道题,是纠结于解题思路的那种困扰吧。...
  • u012814856
  • u012814856
  • 2017年06月02日 16:11
  • 902

【python】【leetcode】【算法题目387—First Unique Character in a String】

一、题目描述 题目原文: Given a string, find the first non-repeating character in it and return it's in...
  • u014615155
  • u014615155
  • 2016年12月04日 14:18
  • 341

387. First Unique Character in a String Java

1、题目来源:点击打开链接 2、题目: Given a string, find the first non-repeating character in it and return it...
  • tzh476
  • tzh476
  • 2016年09月04日 18:37
  • 453

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 ex...
  • ajiangfan
  • ajiangfan
  • 2016年09月22日 11:18
  • 786

LeetCode387—First Unique Character in a String

LeetCode387 First Unique Character in A String
  • zhangxiao93
  • zhangxiao93
  • 2016年08月26日 21:46
  • 923

387. First Unique Character in a String --Python

387. First Unique Character in a String Given a string, find the first non-repeating character in i...
  • Chris_zhangrx
  • Chris_zhangrx
  • 2018年01月22日 22:09
  • 51

first unique character in a string

题目:给定一个string,找出第一个string里的unique character,如“cisco”的话就应该是i,“cat”的话就是c。 第一种方法是用LinkedHashMap保留数据的顺序...
  • u012204343
  • u012204343
  • 2014年09月21日 08:26
  • 1537

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 ex...
  • Al_xin
  • Al_xin
  • 2016年08月22日 22:18
  • 517

Leetcode387. First Unique Character in a String

一、题目 Given a string, find the first non-repeating character in it and return it's index. If it ...
  • weifang0626
  • weifang0626
  • 2016年11月27日 11:54
  • 62

387.leetcode First Unique Character in a String(easy)[统计字符串字符次数]

Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex...
  • qz530308783
  • qz530308783
  • 2016年08月22日 19:51
  • 533
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode387. First Unique Character in a String
举报原因:
原因补充:

(最多只允许输入30个字)