[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure?

这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用。像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数字范围位相与的解法二中也用到过这种方法。由于ASCII表里的基本表共有128个字符,也就是可以用键盘表示出来的,整个表共有256个字符,所以我们只要用一个大小为256的整型数组就可以包含所有的字符,我们遍历输入字符串,对每一个字符都存入到相应位置,并赋值1,如果遇到已经为1的,说明之前出现过该字符,返回false,如果遍历完s,则返回true,代码如下:

class Solution {
public:
    bool isUniqueChar(string s) {
        if (s.size() > 128) return false;
        int m[256] = {0};
        for (int i = 0; i < s.size(); ++i) {
            if (m[s[i]] > 0) return false;
            m[s[i]] = 1;
        }
        return true;
    }
};

书上还给了另一种解法,是用位操作 Bit Manipulation,但是这种解法只有当输入字符串是由小写字母组成的才成立,因为小写字母只有26个,不超过整型int的32位,对于每个字母,我们将对应位置设为1,然后看之前是否是1,是的话返回false,不是的话设为1。跟上面的方法核心是一样的,只不过空间上省了很多,但是也对输入做了更为严格的限制,代码如下:

// Only works when s consists of lower-case letters a-z
class Solution {
public:
    bool isUniqueChar(string s) {
        int m = 0;
        for (int i = 0; i < s.size(); ++i) {
            int d = s[i] - ‘a‘;
            if (m & (1 << d) > 0) return false;
            m |= (1 << d);
        }
        return true;
    }
};
时间: 2024-11-11 02:29:50

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符的相关文章

[LeetCode] First Unique Character in a String 字符串第一个不同字符

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

Javascript --扩展String实现替换字符串中index处字符

String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.length-1-n); } Javascript --扩展String实现替换字符串中index处字符

三. Anagram detection problem for string(字符串中回文词汇检测问题)

anagram 相同字母异序词.heart vs earth 1.Our first solution to the anagram problem will check to see that each character in the first string actually occurs in the second. If it is possible to "checkoff" each character, then the two strings must be anag

js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf

1 var Cts = "bblText"; 2 3 if(Cts.indexOf("Text") > 0 ) 4 { 5 alert('Cts中包含Text字符串'); 6 } indexOf用法:  返回 String 对象内第一次出现子字符串的字符位置.       strObj.indexOf(subString[, startIndex])       参数    strObj       必选项.String 对象或文字.       subStr

[LeetCode] 387. First Unique Character in a String 字符串的第一个唯一字符

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase

[LeetCode] Bold Words in String 字符串中的加粗单词

Given a set of keywords words and a string S, make all appearances of all keywords in S bold. Any letters between <b> and </b> tags become bold. The returned string should use the least number of tags possible, and of course the tags should fo

[Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the startin

[LeetCode] Add Bold Tag in String 字符串中增添加粗标签

Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold

【lua】lua string.match 和 string.split 从字符串中寻找特定字符串并保存

local string = "{1,2,3,4}" local traString=string.match(string , "%d+,%d+,%d+,%d+") --此时tranString = "1,2,3,4",去掉"{","}" string = string.split(tranString , ",") string = {1,2,3,4} string[1]=1 str