CCI习题1-1:String Contain Unique Char
来源:互联网 发布:下载dreamweaver软件 编辑:程序博客网 时间:2024/06/06 02:39
package question1_1;
/*
* Implement an algorithm to determine if a string has all unique characters
*/
public class Question {
//位操作
public static boolean isUniqueChars1(String str){
if(str.length()>256){
return false;
}
//字母二进制左移序号val位(例如a 0,c 2)
//字母X与自身X按位与>0,与其他=0
//checker每一位按位或操作,存储字母a~z是否出现过,相应的1对应相应序号的字母出现过
//只要字母X曾经出现过1次,下一次循环按位与>0
int checker=0;
for(int i=0;i<str.length();i++){
int val=str.charAt(i)-'a';
if((checker&(1<<val))>0)
return false;
checker|=(1<<val);
}
return true;
}
//数组存储
public static boolean isUniqueChars(String str) {
if (str.length() > 256) {
return false;
}
boolean[] char_set = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (char_set[val]) return false;
char_set[val] = true;
}
return true;
}
public static boolean isUniqueChars2(String str){
if (str.length() > 256) {
return false;
}
boolean [] char_set=new boolean[256];
for(int i=0;i<str.length();i++){
int val=str.charAt(i);
if(char_set[val])
return false;
char_set[val]=true;
}
return true;
}
public static void main(String[] args) {
String[] words={"abcde", "hello", "apple", "kite", "padle"};
for(String word:words){
System.out.println(word+":"+isUniqueChars(word)+" "+isUniqueChars2(word));
}
}
}
/*
* Implement an algorithm to determine if a string has all unique characters
*/
public class Question {
//位操作
public static boolean isUniqueChars1(String str){
if(str.length()>256){
return false;
}
//字母二进制左移序号val位(例如a 0,c 2)
//字母X与自身X按位与>0,与其他=0
//checker每一位按位或操作,存储字母a~z是否出现过,相应的1对应相应序号的字母出现过
//只要字母X曾经出现过1次,下一次循环按位与>0
int checker=0;
for(int i=0;i<str.length();i++){
int val=str.charAt(i)-'a';
if((checker&(1<<val))>0)
return false;
checker|=(1<<val);
}
return true;
}
//数组存储
public static boolean isUniqueChars(String str) {
if (str.length() > 256) {
return false;
}
boolean[] char_set = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (char_set[val]) return false;
char_set[val] = true;
}
return true;
}
public static boolean isUniqueChars2(String str){
if (str.length() > 256) {
return false;
}
boolean [] char_set=new boolean[256];
for(int i=0;i<str.length();i++){
int val=str.charAt(i);
if(char_set[val])
return false;
char_set[val]=true;
}
return true;
}
public static void main(String[] args) {
String[] words={"abcde", "hello", "apple", "kite", "padle"};
for(String word:words){
System.out.println(word+":"+isUniqueChars(word)+" "+isUniqueChars2(word));
}
}
}
- CCI习题1-1:String Contain Unique Char
- CCI习题 1-2:Reverse String
- CCI 习题笔记 1
- career cup1.1 unique char
- Array Question 1 unique string
- Check if the string has unique char.
- Find the first unique char in string
- [cc一百五]Unique char in String
- cci-Q2.1 未排序链表去重
- CCI(1)-- Arrays and Strings
- Python习题1:None-Unique Elements 习题+前三名答案解析(checkio.org)
- CString/string/char *比较详解1
- cci-Q1.1 判断字符串中字符是否唯一
- cci-Q3.1 一个数组实现三个栈
- 【1】Detect if a string has all unique characters
- C++primer学习:string的操作习题(1):
- cannot convert parameter 1 from 'std::string' to 'const char *'
- c/c++数据类型转换1(float,char,string,CString )
- 解决strus2中文乱码
- audio system分析之audiotrack.java
- Hadoop安装端口已经被占用问题的解决方法
- google面试题
- leetcode7: Unique Binary Search Trees
- CCI习题1-1:String Contain Unique Char
- linux下安装apache
- 互斥锁Mutex与信号量Semaphore的区别
- 评教系统需求了解
- 枚举串口四法
- Android用户界面之notifaction(状态栏通知)
- 软件开发流程来自百度
- GDI+的使用
- 数组--结构体数组