给定一个字符串,请你判断其中每个字符是否全都不同。
数据范围:字符串长度满足
import java.util.*; public class Solution { Map<Character,Character> map=new HashMap<>(); public boolean isUnique (String str) { for(int i=0;i<str.length();i++){ if(map.get(str.charAt(i))!=null){ return false; }else{ map.put(str.charAt(i),'a'); } } return true; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return bool布尔型 */ public boolean isUnique (String str) { // write code here long last = 0; for(int i = 0; i < str.length(); i++) { long count = last ^ (1L << (str.charAt(i) - 'a')); if(count < last) return false; last = count; } return true; // long count = 0; // for (int i = 0; i < str.length(); i++) { // int shift = str.charAt(i) - 'a'; // 假设字符串只包含小写字母 // if ((count & (1L << shift)) != 0) return false; // 检查对应位是否已经置为1 // count |= (1L << shift); // 设置该位 // } // return true; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return bool布尔型 */ public boolean isUnique (String str) { // write code here if(str.length()==0){ return true; } ArrayList<Character> list1=new ArrayList<>(); for(int i=0;i<str.length();i++){ if(!list1.contains(str.charAt(i))){ list1.add(str.charAt(i)); }else{ return false; } } return true; } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return bool布尔型 */ int cmp(const void* e1,const void* e2){ return *((char*)e1)-*((char*)e2); } bool isUnique(char* str ) { // write code here qsort(str,strlen(str),1,cmp); for(int i=1;i<strlen(str);i++){ if(str[i]==str[i-1]) return false; } return true; }
class Solution: def isUnique(self , str: str) -> bool: # write code here a= set(list(str)) if len(a) == len(str): return True return False
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return bool布尔型 */ bool isUnique(string str) { // write code here int big = 0, small = 0; for(int i = 0; i < str.size(); ++i){ int mv = 1; if(str[i] <= 'Z') { int n = str[i] - 'A'; while(n--) mv = mv << 1; if((big & (mv)) != 0) return false; big |= mv; }else{ int n = str[i] - 'a'; while(n--) mv = mv << 1; if((small & (mv)) != 0) return false; small |= mv; } } return true; } };