LeetCode 387. First Unique Character in a String
来源:互联网 发布:精索静脉曲张程度数据 编辑:程序博客网 时间:2024/06/06 20:38
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 = “loveleetcode”,
return 2.
Note: You may assume the string contain only lowercase letters.
Code
class Solution {public: int firstUniqChar(string s) { if (s.size() == 1) return 0; if (s == "") return -1; int index[26]={0}; for (int i=0;i<s.size();i++) index[s[i]-97] = index[s[i]-97]==0?i+1:s.size()+1; int min=s.size()+1; for (int i:index) if (i != 0 && i < min) min = i; if (min != s.size() + 1) return min-1; return -1; }};
Appendix
- Link: https://leetcode.com/problems/first-unique-character-in-a-string/
- Run Time: 59ms
0 0
- [Leetcode]First Unique Character in a String
- LeetCode: First Unique Character in a String
- LeetCode-First Unique Character in a String
- leetcode---First Unique Character in a String
- leetcode:First Unique Character in a String
- LeetCode-First Unique Character in a String
- [Leetcode] First Unique Character in a String
- leetcode First Unique Character in a String
- Leetcode First Unique Character in a String
- LEETCODE--First Unique Character in a String
- LeetCode -- First Unique Character in a String
- LeetCode : First Unique Character in a String
- LeetCode First Unique Character in a String
- LeetCode-----First Unique Character in a String
- LeetCode—387. First Unique Character in a String
- 【leetcode】387. First Unique Character in a String【E】
- <LeetCode OJ> 387. First Unique Character in a String
- [leetcode] 387. First Unique Character in a String 解题报告
- SOJ 1043
- 区域生长算法
- python: extend(),append(),insert()
- JS代码规范笔记
- poj 3304 Segments
- LeetCode 387. First Unique Character in a String
- 虚函数、纯虚函数在C++和java中的区别
- JS-笔记
- 回调函数总结
- Intent 简单使用
- 使用闭包
- Mootools试用体会
- cifar-10数据转为图片(jpg,png)
- Mybatis源码解析四、SqlSession运行过程