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() > ) return false;
int m[] = {};
for (int i = ; i < s.size(); ++i) {
if (m[s[i]] > ) return false;
m[s[i]] = ;
}
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 = ;
for (int i = ; i < s.size(); ++i) {
int d = s[i] - 'a';
if (m & ( << d) > ) return false;
m |= ( << d);
}
return true;
}
};

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

  1. [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 ex ...

  2. LeetCode 387. First Unique Character in a String (字符串中的第一个唯一字符)

    题目标签:String, HashMap 题目给了我们一个 string,让我们找出 第一个 唯一的 char. 设立一个 hashmap,把 char 当作 key,char 的index 当作va ...

  3. 387 First Unique Character in a String 字符串中的第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...

  4. LeetCode387First Unique Character in a String字符串中第一个唯一字符

    给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...

  5. String 字符串中含有 Unicode 编码时,转为UTF-8

    1.单纯的Unicode 转码 String a = "\u53ef\u4ee5\u6ce8\u518c"; a = new String(a.getBytes("UTF ...

  6. 将string字符串中的换行符进行替换

    /** * 方法名称:replaceBlank * 方法描述: 将string字符串中的换行符进行替换为"" * */ public static String replaceBl ...

  7. [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 le ...

  8. [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 ...

  9. [LeetCode] Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

随机推荐

  1. http 缓存相关学习

    在面试中被问到好多缓存的问题  200 cache cookies 304(304 和 200 cache的区别) cookies在什么时候创建  发送  等等  自己回答的并不是很好 这次仔细的学习 ...

  2. Javascript实现格式化输出

    前两天看面试题,其中有一道要实现js的格式化输出,具体给出的是: Javascript实现格式化输出,比如输入999999999,输出为999,999,999 我的实现方式是 function for ...

  3. UITableView优化

    作为iOS开发,UITableView可能是平时我们打交道最多的UI控件之一,其重要性不言而喻. 关于TableView,我想最核心的就是UITableViewCell的重用机制了. 简单来说呢就是当 ...

  4. ArcGIS中国工具应用:固定比例尺固定纸张批量打印

    ArcGIS中国工具应用:固定比例尺固定纸张批量打印 下载地址:http://files.cnblogs.com/files/gisoracle/a3batchprint.zip 固定A3,比例尺1: ...

  5. iOS 开发中的争议(一)

    序言 打算分享一些有争议的话题,并且表达一下我的看法.这是该系列的第一篇,我想讨论的是:类的成员变量应该如何定义? 在 Objective-C 的语言的早期,类的私有成员变量是只能定义在 .h 的头文 ...

  6. 带交互的 iOS 产品原型可以用什么软件制作?

    摘自知乎http://www.zhihu.com/question/20326729 来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 首先如果你小团队或者个人开发,当然 ...

  7. 对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  8. ORA-27125: unable to create shared memory segment

    平台环境   :  Oracle Linux Server release 5.7 x86_64 数据库版本 :  Oracle Database 10g Enterprise Edition Rel ...

  9. SQL SERVER 中如何用脚本管理作业

    在SQL SERVER中用脚本管理作业,在绝大部分场景下,脚本都比UI界面管理作业要高效.简洁.打个简单的比方,如果你要查看作业的运行时长,如果用UI界面查看,100个作业,你就得在历史记录里面至少查 ...

  10. Cannot set a credential for principal 'sa'. (Microsoft SQL Server,错误: 15535)

    在SQL SERVER 2008上上禁用sa登录时,遇到下面错误:"Cannot set a credential for principal 'sa'. (Microsoft SQL Se ...