亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] First Unique Character in a S

Xufc / 3411人閱讀

Problem

Given a string, find the first non-repeating character in it and return it"s index. If it doesn"t exist, return -1.

Example

Given s = "lintcode", return 0.

Given s = "lovelintcode", return 2.

Tags

Amazon Microsoft Bloomberg

Solution
a fast way
class Solution {
    public int firstUniqChar(String s) {
        int[] dict = new int[26];
        for (int i = 0; i < s.length(); i++) {
            dict[s.charAt(i)-"a"]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (dict[s.charAt(i)-"a"] == 1) return i;
        }
        return -1;
    }
}
a dumb way...
public class Solution {
    public int firstUniqChar(String s) {
        //store string in an array
        char[] str = s.toCharArray();
        //use HashMap to check each character"s frequency
        Map map = new HashMap<>();
        for (int i = 0; i < str.length; i++) {
            char ch = str[i];
            //reset duplicate chars to "#"
            if (map.containsKey(ch)) {
                str[map.get(ch)] = "#";
                str[i] = "#";
            } else {
                map.put(ch, i);
            }
        }
        int index = str.length;
        for (int i = 0; i < str.length; i++) {
            //find the first character that is not "#" and return its index
            if (str[i] != "#") {
                return i;
            }
        }
        //if no unique character, return -1
        return -1;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/68097.html

相關(guān)文章

  • [LintCode/LeetCode] Unique Paths II

    摘要:和完全一樣的做法,只要在初始化首行和首列遇到時(shí)置零且即可。對(duì)了,數(shù)組其它元素遇到也要置零喏,不過就不要啦。 Problem Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle...

    firim 評(píng)論0 收藏0
  • [LintCode/LeetCode] Minimum Window Substring

    Problem Given a string source and a string target, find the minimum window in source which will contain all the characters in target. Notice If there is no such window in source that covers all charac...

    Corwien 評(píng)論0 收藏0
  • [LintCode/LeetCode] 3Sum

    摘要:雙指針法的解法。然后用和夾逼找到使三數(shù)和為零的三數(shù)數(shù)列,放入結(jié)果數(shù)組。對(duì)于這三個(gè)數(shù),如果循環(huán)的下一個(gè)數(shù)值和當(dāng)前數(shù)值相等,就跳過以避免中有相同的解。 Problem Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplet...

    Sunxb 評(píng)論0 收藏0
  • [LintCode/LeetCode] Unique Paths

    摘要:簡(jiǎn)單的動(dòng)規(guī)題目,建立數(shù)組。坐標(biāo)為矩陣的坐標(biāo),值為從左上角到這一格的走法總數(shù)。賦初值,最上一行和最左列的所有格子的走法都只有一種,其余格子的走法等于其左邊格子走法與上方格子走法之和。最后,返回即可。 Problem A robot is located at the top-left corner of a m x n grid (marked Start in the diagram ...

    Gu_Yan 評(píng)論0 收藏0
  • [LintCode/LeetCode] Two Strings are Anagrams/Valid

    摘要:建立一個(gè)長(zhǎng)度為的數(shù)組,統(tǒng)計(jì)所有個(gè)字符在出現(xiàn)的次數(shù),然后減去這些字符在中出現(xiàn)的次數(shù)。否則,循環(huán)結(jié)束,說明所有字符在和中出現(xiàn)的次數(shù)一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....

    vslam 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<