Unique Charcters

来源:互联网 发布:客户端怎么连接数据库 编辑:程序博客网 时间:2024/06/15 01:52
//2017.5.2


1. Unique Charcters
description: Implement an algorithm to determine if a string has all unique characters.


public class Solution {
    /**
     * @param str: a string
     * @return: a boolean
     */
    public boolean isUnique(String str) {
        // write your code here
        if (str == null || str.length() == 0) {
            return false;
        }
        if (str.length() > 256) {
            return false;
        }
        boolean[] arr = new boolean[256];
        for (int i = 0; i < str.length(); i++) {
            if (arr[str.charAt(i)]) {
                return false;
            }
            arr[str.charAt(i)] = true;
        }
        return true;
    }
}


解析:假设使用的是ASCII编码方式,直接使用256个数组就可以非常迅速的处理。
0 0
原创粉丝点击