Skip to content

leetcode-0387 字符串中的第一个唯一字符 #103

@GuoLizhi

Description

@GuoLizhi

题目地址 https://leetcode-cn.com/problems/first-unique-character-in-a-string/

Hash Table

时间复杂度O(n) 空间复杂度O(n)

var firstUniqChar = function(s) {
    let map = {};
    for (let i = 0; i < s.length; i++) {
        const c = s[i];
        map[c] = map[c] !== undefined ? map[c] + 1 : 1;
    }
    for (let i = 0; i < s.length; i++) {
        if (map[s[i]] === 1)
            return i;
    }
    return -1;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions