博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cracking the Coding Interview Q1.1
阅读量:4469 次
发布时间:2019-06-08

本文共 3182 字,大约阅读时间需要 10 分钟。

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

My Solution:

package chapter1;/** * Implement an algorithm to determine if a string has all unique characters. * What if you can not use additional data structures? *  * @author jd *  */public class Q1_1 {    /**     * if the char set is extended ASCII(256 characters).     *      * Time complexity is O(n), where n is the length of the string, and space     * complexity is O(1).     *      */    public static boolean isUniqueChars(String str) {        if (str == null || str.length() <= 1)            return true;        boolean[] exist = new boolean[256];        for (int i = 0; i < str.length(); i++) {            if (exist[str.charAt(i)] == false)                exist[str.charAt(i)] = true;            else                return false;        }        return true;    }    /**     * we can reduce the space usage by using a bit vector     *      */    public static boolean isUniqueChars2(String str) {        if (str == null || str.length() <= 1)            return true;        byte[] exist = new byte[32];        for (int i = 0; i < str.length(); i++) {            int idx = str.charAt(i);            if ((exist[idx / 8] & (1 << (idx % 8))) == 0)                exist[idx / 8] |= 1 << (idx % 8);            else                return false;        }        return true;    }    public static void main(String[] args) {        String[] words = { "abcde", "hello", "apple", "kite", "padle", "aa", "abba" };        for (String word : words) {            System.out.println(word + ": " + isUniqueChars(word) + " " + isUniqueChars2(word));        }    }    /**     * alternative solutions:      * 1. Check each char of the string with every other     * char of the string for duplicate occurrences. This will take O(n^2) and     * no space.      * 2. If we are allowed to destroy the string, we can sort the     * chars in the string in O(nlogn) time and linearly check the string for     * neighboring chars that are identical.      */}
View Code

 

Solution:

package Question1_1;public class Question {    public static boolean isUniqueChars(String str) {        if (str.length() > 256) {            return false;        }        int checker = 0;        for (int i = 0; i < str.length(); i++) {            int val = str.charAt(i) - 'a';            if ((checker & (1 << val)) > 0) return false;            checker |= (1 << val);        }        return true;    }        public static boolean isUniqueChars2(String str) {        if (str.length() > 256) {            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;            char_set[val] = true;        }        return true;    }        public static void main(String[] args) {        String[] words = {"abcde", "hello", "apple", "kite", "padle"};        for (String word : words) {            System.out.println(word + ": " + isUniqueChars(word) + " " + isUniqueChars2(word));        }    }}
View Code

 

转载于:https://www.cnblogs.com/jdflyfly/p/3821655.html

你可能感兴趣的文章
微信小游戏入门
查看>>
python 首次安装 报错
查看>>
人工智能岗位替代----厨师
查看>>
poj 1237 The Postal Worker Rings Once
查看>>
Java基础学习笔记八 Java基础语法之接口和多态
查看>>
程序员修炼之道-阅读笔记02
查看>>
CSV模块
查看>>
英文词频统计预备,组合数据类型练习
查看>>
工厂模式
查看>>
java servlet 中文乱码
查看>>
数据的描述性统计
查看>>
一对多sql
查看>>
AntDesign vue学习笔记(七)Form 读写与图片上传
查看>>
想做公众号,总要写点什么--第008期博文
查看>>
打印某个字符串出现的次数。(新手)
查看>>
mysql 管理
查看>>
Codeforce 1175 D. Array Splitting
查看>>
03.html学习-表格
查看>>
Java反射
查看>>
驱动精灵扩展版(集成万能网卡驱动)无法自动识别网卡的解决方案
查看>>