# 387 First Unique Character in a String (有圖)
應用桶子排序 / 建立桶子 lookup = new Array(26); lookup.fill(0)
Given a string,
find the first non-repeating character in it and return it's index.
If it doesn't exist, return -1.
input: 給一字串
output: 找出第一個沒有重覆的字元Examples:
s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.
/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function(s) {
};如何解
丟入 Map 然後抓第一個 length = 1 的值 (Map 有順序性)
結果速度只有 14% 好慘
改善
這題應用到桶子排序,桶子排序就是抓取速度超快,但容易浪費記憶體.不過這邊因為要存字母,字母也才 26 個數字而已

遍歷一次 s 然後把字母放到桶子

學到什麼
這題學到蠻多,多一個新的解題方式了就是應用桶子排序解,主要就是可以先把桶子建立起來再存相對應數量進去
Last updated
Was this helpful?