career cup1.1 unique char

1. Hash map initialization is like this:

HashMap<K, V> map = new HashMap<K, V>();
K,V can only be class type, not primitive. So when you want to use int or char, use Integer and Character instead.
Converting from int to Integer: new Integer(i); Integer -> int: Integer.intValue()
Converting from char to Character: new Character(c); Character -> char: Character.charValue();
It seems now java will automatically convert for you.

   tips: HashMap uses too much spaces. Change to array of boolean, boolean[] checker[i] (i is the int value of char)

2. details
(1) unicode or ascii code
(2) if it is ascii code, it only has 256 chars. So if the string length is larger than 256, return false directly
(3) what if the given string is null or empty? Deal with it in your code

3. bit implementation
(1) byte 8, short 16, int 32, long 64, char 16
(2) use BitSet, set ith bit to indicate that char i has appeared before
(3) use bit manipulation. When char appears, change the ith bit to 1(i = the int value of char)

4. if do not use additional data structure
(1) if allowing to modify string, sort it.   
(2) if not allow, naively brute force o(n^2)

tips: string is immutable in java, use StringBuilder instead. StringBuilder is faster than StringBuffer, because StringBuilder is not synchronized
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值