| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + /** | ||
| 2 | + * Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. | ||
| 3 | + * <p> | ||
| 4 | + * Examples: | ||
| 5 | + * <p> | ||
| 6 | + * s = "leetcode" | ||
| 7 | + * return 0. | ||
| 8 | + * <p> | ||
| 9 | + * s = "loveleetcode", | ||
| 10 | + * return 2. | ||
| 11 | + */ | ||
| 12 | + public class FirstUniqueCharacter { | ||
| 13 | + | ||
| 14 | + public int firstUniqChar(String s) { | ||
| 15 | + char[] chars = s.toCharArray(); | ||
| 16 | + for (int i = 0; i < chars.length; i++) { | ||
| 17 | + char word = chars[i]; | ||
| 18 | + if (!isContainsWord(chars, word, i)) { | ||
| 19 | + return i; | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + return -1; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + private boolean isContainsWord(char[] words, char word, int currPos) { | ||
| 26 | + for (int i = 0; i < words.length; i++) { | ||
| 27 | + if (currPos == i) { | ||
| 28 | + continue; | ||
| 29 | + } | ||
| 30 | + if (words[i] == word) { | ||
| 31 | + return true; | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + return false; | ||
| 35 | + } | ||
| 36 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments