成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Unique Characters

DevWiki / 2697人閱讀

摘要:用做,十分簡單。如果不使用之外的數(shù)據(jù)結(jié)構(gòu)的話,可以用來做。不過只能過純字母字符的。

Problem

Implement an algorithm to determine if a string has all unique characters.

Example

Given "abc", return true.

Given "aab", return false.

Challenge

What if you can not use additional data structures?

Note

用HashSet做,十分簡單。
如果不使用String之外的數(shù)據(jù)結(jié)構(gòu)的話,可以用bit manipulation來做。不過只能過純字母字符的testcase。

Solution
public class Solution {
    public boolean isUnique(String str) {
        HashSet set = new HashSet();
        for (int i = 0; i < str.length(); i++) {
            if (!set.contains(str.charAt(i))) {
                set.add(str.charAt(i));
            }
            else return false;
        }
        return true;
    }
}
public class Solution {
    public boolean isUnique(String s) {
        int checker = 0;
        for (int i = 0; i < s.length(); i++) {
            int cur = s.charAt(i) - "a";
            if ((checker & (1 << cur)) > 0) {
                return false;
            }
            checker |= 1 << cur;
        }
        return true;
    }
}

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

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65694.html

相關文章

  • [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 評論0 收藏0
  • [LintCode] String Homomorphism

    Problem Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with an...

    tanglijun 評論0 收藏0
  • [LintCode] Compare Strings

    摘要:建立一個長度為的數(shù)組,每一位對應那個字母出現(xiàn)的個數(shù),先遍歷,對數(shù)組做增操作,再遍歷,對數(shù)組做減操作。 Problem Compare two strings A and B, determine whether A contains all of the characters in B. The characters in string A and B are all Upper Ca...

    avwu 評論0 收藏0
  • [LintCode/LeetCode] Unique Paths II

    摘要:和完全一樣的做法,只要在初始化首行和首列遇到時置零且即可。對了,數(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 評論0 收藏0
  • [LintCode/LeetCode] First Unique Character in a S

    Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...

    Xufc 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<