判斷字符串的字符是否重復


原文:Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

譯文:實現一個算法來判斷一個字符串中的字符是否唯一(即沒有重復).不能使用額外的數據結構。 (即只使用基本的數據結構)

解答:首先需要確定構成字符串的字符集有多大,是ASCII還是26個英文字母,對於不同的字符集有不同的解法。

  以字符集是ASCII為例,我們可以使用一個256大小的boolean數組,數組初始化為false,遍歷一遍字符串中的字符,當bool數組對應位置的值為真, 表明該字符在之前已經出現過,即可得出該字符串中有重復字符。否則將該位置的bool數組 值置為true。

package test;public class Test { public static void main(String args[]){ System.out.println(isRepeat("abcd")); } public static boolean isRepeat(String s){ /** * 該數組默認是false */
        boolean[] bool = new boolean[256]; /** * 遍歷一遍字符串字節 * 當bool數組對應位置的值為真, 表明該字符在之前已經出現過, * 即可得出該字符串中有重復字符, * 否則將該位置的bool數組值置為true。 */
        for(byte a:s.getBytes()){ if(bool[a]) return true; else bool[a] = true; } return false; } }

  這是一種解題思路,還可以通過位向量來減少空間的使用量。對於ASCII字符,我們需要256位,一個int型的數有4個字節,也就是32位,即一個長度為8的int 數組a即可。這里的關鍵是要把字符對應的數字,映射到正確的位上去。a[0]表示第1~32個數(0~31),a[1]表示第33~64個數(32~63)······

 

package test; public class Test { public static void main(String args[]){ System.out.println(isRepeat("abcdsa")); } public static boolean isRepeat(String s){ /** * 該數組默認是false */
        int[] ints = new int[8]; final int CONSTANT = 32; for(byte a:s.getBytes()){ //商,判斷在哪個數組
            int ids = a / CONSTANT; //余數,判斷在數組的第幾位
            int shift = a % CONSTANT; /** * 當前位是1,則返回true,說明重復 * 當前位是0,則置為1。 */
            if((ints[ids] & 1<< shift) > 0) return true; else ints[ids] =  ints[ids] | 1<< shift; } return false; } }

 

關於位向量的幾篇文章:

如何使用位邏輯運算來實現位向量的理解

Java位向量的實現原理與巧妙應用

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM