1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.

思路: 假设给定字符串用的是ASCII编码,那么总共就只有256个字符,新建一个256个元素的boolean数组, 遍历字符串,将出现的字符在boolean数组所在位置置 1。如果碰到已经置一,表明出现重复字符,返回false。

public class IsUniqueChars_1 {

    public boolean solu(String s) {
        boolean table[] = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            int ascii = (int) s.charAt(i);
            if (table[ascii])
                return false;
            else
                table[ascii] = true;
        }
        return true;
    }
}

优化:如果字符串长度超过256,肯定出现过重复字符了。这是只要直接返回 false 就行

public class IsUniqueChars_1 {

    public boolean solu(String s) {
        if (s.length() > 256)
            return false;
        boolean table[] = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            int ascii = (int) s.charAt(i);
            if (table[ascii])
                return false;
            else
                table[ascii] = true;
        }
        return true;
    }
}

2015-09-16

时间: 2024-10-14 00:50:04

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.的相关文章

Determine if a string has all unique characters

Problem: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? <pre name="code" class="cpp">#include<iostream> #include<set> #include<string&

cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.

2.6Given a circular linked list,  implement an algorithm which returns the node at the beginning of the loop. 快指针和慢指针一起在头指针开始移动,快指针每次移动两步,慢指针每次移动一步,直到相遇, 相遇后,把慢指针指向头指针,两个指针一起往后移动一步.直到相遇,相遇的地方就是循环的开始(如果相遇,说明是有循环的节点,这时快指针走的路程是慢指针的两倍,快:开始的k距离,和一圈(或者n圈)循

How to implement an algorithm from a scientific paper

Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scientific paper. I have implemented many complex algorithms from books and scientific publications, and this article sums up what I have learned while se

Algorithm delete some charactors from string

bool IsCharBelongString( IN const char* szIn, IN char ch ) { if (szIn) { while (*szIn) { if (ch == *szIn) { return true; } szIn++; } } return false; } void DeleteChar( IN char* szIn, IN const char* szDelete ) { if (szIn) { char* pStep1 = szIn; char*

Careercup | Chapter 1

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? 字符串问题,需要先确定是不是只有ASCII码. 如果是,可以用char[256],也可以用位向量.位向量的实现参照<编程珠玑>.i&MASK就是取余.i>>SHIFT就是取商. 1 class BitVector {

string类问题总结

1. Unique Characters of a String 字符串中不同的字符 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 思路分析:这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数

CC150-Array and string 1.1

Promble: Implement an algorithm to determine if a string has all unique characters. What if you cannot use addtional data structure? My solution: 1.ckeck wether the length of str is really equal to 0 or more than 256, if yes, it will return false 2.

Cracking the coding interview汇总目录

很久之前刷的CTCI的题目,都快忘记了,做个分类汇总,再重新好好复习一遍. Chapter 1 | Arrays and Strings 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 1.2 Write code to reverse a C-Style String. (C-Str