1.1 Implement an algorithm to determine if a string has all unique characters.
2015-01-16 21:10
399 查看
Implement an algorithm to determine if a string has all unique characters.What if you can not use additional data structures?
思路:首先,字符串是ascii编码还是unicode编码,这会影响记录的数组。 用bool ch[256] 记录所有的字符,初始化为false。遇到出现的字符就置为true,遍历结束字符串之后,说明正确。其实就是利用哈希表。
如果字符比较少,例如只是'a-z',那么可以用bit位的方法,思路和上面相同,只是节省了空间。
思路:首先,字符串是ascii编码还是unicode编码,这会影响记录的数组。 用bool ch[256] 记录所有的字符,初始化为false。遇到出现的字符就置为true,遍历结束字符串之后,说明正确。其实就是利用哈希表。
#include<iostream> #include<string> using namespace std; bool isUnique(string s){ bool ch[256]; fill_n(&ch[0], 256, false); for (int i = 0; i < s.size(); i++) { if (ch[s[i]]){ return false; } ch[s[i]] = true; } return true; } int main() { string s = "abcdefg"; string s1 = "adaf"; cout<<isUnique(s)<<"and"<<isUnique(s1)<<endl; return 0; }
如果字符比较少,例如只是'a-z',那么可以用bit位的方法,思路和上面相同,只是节省了空间。
#include<iostream> #include<string> using namespace std; bool isUnique(string s){ bool ch[256]; fill_n(&ch[0], 256, false); for (int i = 0; i < s.size(); i++) { if (ch[s[i]]){ return false; } ch[s[i]] = true; } return true; } bool isUnique2(string s) { int check = 0; for (int i = 0; i < s.size(); i++) { int val = s[i] - 'a'; if (check>>val&1){ return false; } check |= (1<<val); } return true; } int main() { string s = "abcdefg"; string s1 = "adaf"; cout<<isUnique(s)<<"and"<<isUnique(s1)<<endl; cout<<isUnique2(s)<<"and"<<isUnique2(s1)<<endl; return 0; }
相关文章推荐
- 1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
- Determine if a string has all unique characters
- determine if a string has all unique characters
- 【1】Detect if a string has all unique characters
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
- how to determine if an iOS device has a cellular radio
- how to determine if an iOS device has a cellular radio
- How To Detect If an Application Has Stopped Responding
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- Unable to determine if the owner (Domain\UserName) of job JOB_NAME has server access
- tomcat 7 WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Check if the string has unique char.
- Implement atoi to convert a string to an integer.
- 【软件自动化测试 - VBScript 7】= Using Locate to determine if specific text exists within a string
- How to implement an algorithm from a scientific paper
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- [CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符
- Using BitSet to to judge if string a contains all letters in string b