判断是否有重复字符bool IsUniqueChars( String str) CC150 1.1

    xiaoxiao2023-03-24  5

    1.1 Implement an algorithm to determine if a string has all unique characters. What if you connot use additional  data structures? ASCII 与 UNICODE的区别? http://zhidao.baidu.com/link?url=EAwBwFRrf3vHB8bNd5v0OybwlZq1CxdXP-UenXxTVmx-oPn4kpnK3y2Ro3yjt5gjs9JCADmf9Pa8m38xONtRlXtWqzHRapRGwSW-dzv57Ka ASCII ,ASCII码由一个字节中的7位(bit)表示,范围是0x00 - 0x7F 共128个字符。 扩展ASCII码。范围是0x00 - 0xFF 共256个字符; Unicode, 6万多个。 60 thousand. 思路1 建立定长bool[], 存放是否已经存在这个char 思路2 对比Str中每个char,判断是否存在重复; 缺点:时间复杂度:O(N^2) 思路3 先排序,然后对比周围的元素是否存在相同的。时间复杂度:O(nlogn) ASCII码版本: 时间复杂度:O(N); 空间复杂度:O(N) public static bool IsUniqueChars( String str) { if (str.Length > 128) return false; bool[] charSet = new bool[128]; char[] strArr = new char[str.Length]; strArr = str.ToCharArray(); for ( int i = 0; i < str.Length; i++) { int val = strArr[i]; //返回ASSIC if (charSet[val]) //已经存在这个char { return false; } charSet[val] = true; } return true; }
    转载请注明原文地址: https://ju.6miu.com/read-1202759.html
    最新回复(0)