Leetcode: First Unique Character in a String
LeetCode string in First unique character
2023-09-11 14:14:07 时间
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.
1 public class Solution { 2 public int firstUniqChar(String s) { 3 int[] count = new int[26]; 4 for (int i=0; i<s.length(); i++) { 5 count[s.charAt(i) - 'a']++; 6 } 7 for (int i=0; i<s.length(); i++) { 8 if (count[s.charAt(i) - 'a'] == 1) 9 return i; 10 } 11 return -1; 12 } 13 }
相关文章
- Leetcode: Find All Anagrams in a String
- Leetcode: Reverse String
- Leetcode: Verify Preorder Sequence in Binary Search Tree
- Leetcode: Reverse Words in a String
- Leetcode: String to Integer
- C#String.Split (string[], StringSplitOptions) 多参数分割得到数组
- [LeetCode] Reverse Words in a String
- 【LeetCode】211. Add and Search Word - Data structure design
- 【LeetCode】151. Reverse Words in a String
- leetcode - Interleaving String
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
- LeetCode 15. 三数之和
- [LeetCode] 1047. Remove All Adjacent Duplicates In String 移除字符串中所有相邻的重复字符
- [LeetCode] 1129. Shortest Path with Alternating Colors 颜色交替的最短路径
- [LeetCode] 1016. Binary String With Substrings Representing 1 To N 子串能表示从1到N数字的二进制串
- [LeetCode] 880. Decoded String at Index 在位置坐标处解码字符串
- [LeetCode] Valid Parenthesis String 验证括号字符串
- [LeetCode] 647. Palindromic Substrings 回文子字符串
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
- [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
- [LeetCode] Number of Segments in a String 字符串中的分段数量
- [LeetCode] 75. Sort Colors 颜色排序
- leetcode 695. Max Area of Island 岛屿的最大面积(中等)