码迷,mamicode.com
首页 > 其他好文 > 详细

[Lintcode 1 easy]Unique Characters

时间:2015-11-05 06:22:37      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

【Problem】

Unique Characters

Implement an algorithm to determine if a string has all unique characters.

 
Example

Given "abc", return true.

Given "aab", return false.

Challenge

What if you can not use additional data structures?

---------------------------------------------------------------------------------------------------------------------

 

This problem is from <crack the coding interview>.

 

It is used to test the basic knowledge of ASCII.

If we didn‘t care about how much space we used, basic idea is to set a flag for every single char in the string. If the char has appeared once, return false.

Following is the code:

public class Solution {
    /**
     * @param str: a string
     * @return: a boolean
     */
    public boolean isUnique(String str) {
        
        // write your code here
        if(str.length()>128) 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;
            }
            else
            {
                char_set[val]=true;
            }
        }
        return true;
       
    }
}

 

 

 

[Lintcode 1 easy]Unique Characters

标签:

原文地址:http://www.cnblogs.com/kittyamin/p/4937975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!