问题:实现算法以确定字符串是否具有所有唯一字符。如果您不能使用其他数据结构,该怎么办? [英] Questions:Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure

查看:158
本文介绍了问题:实现算法以确定字符串是否具有所有唯一字符。如果您不能使用其他数据结构,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:

public 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;
}

我有一些问题:


  1. 代码基于给定字符串是ASCII字符串的前提,如果它是Unicode字符串,该怎么办? ASCII字符串和Unicode字符串有什么区别?

  1. The codes is based on the premise that the given string is a ASCII string, what if it is a Unicode string? What's the difference between ASCII string and Unicode string?

int val = str.charAt(i)
我搜索了ASCII表,

int val=str.charAt(i) I searched ASCII table,

如果字符串是@ABCD,将返回什么号码?(将使用三个Dec,Hx,Oct中的哪一个?)

If the string is "@ABCD", what number will return?(which of the three"Dec, Hx, Oct" will be use?)


  1. str.charAt(i)应该返回一个字符(例如A),为什么将数据类型声明为int可以直接将字符转换为相应的ASCII表的数字?


推荐答案


str.charAt(i)应返回一个字符(例如A),为什么将数据类型声明为int可以直接将字符转换为相应的ASCII表的数字?

The str.charAt(i) should return a character (e.g. A), why declaring the data type as int can directly convert the character to the accordingly ASCII table's number?

当你写 int i = str.charAt(...),Java语言规范称为扩展原始转换。您可以这样做,因为 int char 都是类似整数的数字类型,并且每个合法字符值都是也是一个合法的int值。

When you write int i = str.charAt(...), the Java language spec calls that Widening Primitive Conversion. You are allowed to do it because int and char are both integer-like numeric types, and every legal char value is also a legal int value.

http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#25214

至于为什么使用ASCII?这是因为Unicode是为了兼容US-ASCII字符集而故意设计的。对于ASCII中的每个字符,它的Unicode编码与其ASCII编码具有相同的数值。

As for why ASCII? It's because Unicode was deliberately designed for compatibility with the US-ASCII character set. For every character in ASCII, it's Unicode encoding has the same numeric value as its ASCII encoding.

三者中的哪一个 Dec,Hx,Oct将被使用?

which of the three"Dec, Hx, Oct" will be use?

Dec,Hx和Oct列中的数字与使用不同的地点 - 值基数表示的数字相同。以字母'A'为例。当您以十进制表示A的数字代码时,它为65。在基数16中表示的相同数字是41,而在基数8中它是101。该表给出了所有三个基础的代码,因为这三个代码通常用于计算机程序,以及计算机硬件和软件的文档中。

The numbers in the Dec, Hx, and Oct columns are just the same number represented with different place-value bases. Take the letter 'A' for example. When you express the numeric code for 'A' in decimal, it's "65". The same number expressed in base 16 is "41", and in base 8 it's "101". The table gives the codes in all three bases because all three are commonly used in computer programs, and in the documentation of computer hardware and software.

Base 8和base 16是通常使用,因为所有现代计算机都代表基数为2的数字,并且在基数2和基数8或基数16之间进行转换非常容易。

Base 8 and base 16 are commonly used because all modern computers represent numbers in base 2, and it's trivially easy to convert between base 2 and base 8 or base 16.

http://en.wikipedia.org/wiki/Hexadecimal

http://en.wikipedia.org/wiki/Octal

这篇关于问题:实现算法以确定字符串是否具有所有唯一字符。如果您不能使用其他数据结构,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆