[英]Cracking the coding Interview: Unique Character (Working of if block)
I want to understand the working of if block from the below code.我想从下面的代码中了解 if 块的工作原理。 For example: if my string is rock, then the ASCII would be
char_set[r] = 114
char_set[o] = 111
, char_set[c] = 99
, char_set[k] = 107
.例如:如果我的字符串是摇滚,那么 ASCII 将是
char_set[r] = 114
char_set[o] = 111
, char_set[c] = 99
, char_set[k] = 107
。 So for the first time when char_set[r] = 114
will come to if block then what is going to be checked?那么第一次当
char_set[r] = 114
会来if block 那么要检查什么? Since char_set
is boolean type so it could be either true or false but according to my understanding initially char_set[r] = 114
is not set to either true or false.由于
char_set
是布尔类型,所以它可能是真或假,但根据我的理解,最初char_set[r] = 114
没有设置为真或假。 So how can we check if it is either true or false inside the if block.那么我们如何在 if 块中检查它是真还是假。 I have looked at the previous related questions but my question was different and couldn't find anything.
我查看了之前的相关问题,但我的问题不同,找不到任何内容。 This question is taken from the book Cracking the coding interview.
这个问题取自《破解编码面试》一书。
public boolean isuniqueChars2(String str) {
if (str.length() > 128)
return false;
boolean[] char_set = new boolean[256];
for (int i = 1; i < str.length(); i++)
{
int val = str.charAt(i);
if (char_set[val]) {
return false;
}
char_set[val] = true;
}
return true;
}
Arrays are initialized with default values upon creation.数组在创建时使用默认值进行初始化。 Default value for
boolean
type is false
. boolean
类型的默认值为false
。
What char_set
does is for every ASCII value, it stores a boolean
value for whether you've seen that character before or not. char_set
作用是针对每个 ASCII 值,它存储一个boolean
值,表示您之前是否见过该字符。
So essentially, it is saying this (in somewhat pseudocode):所以本质上,它是这样说的(有点伪代码):
for (every character in the string) {
int val = str.charAt(i); //get the current character;
if (char_set[val]) // if (we've seen the current character already)
return false; // not made of unique characters
char_set[val] = true; //store that we've seen the current character;
}
return true; // we got through all the characters, so it must now be unique.
What is really happening is that if your input string is rock
, then char_set['r']
is true
, and so is char_set['o']
, char_set['c']
, and char_set['k']
.真正发生的情况是,如果您的输入字符串是
rock
,则char_set['r']
是true
, char_set['o']
、 char_set['c']
和char_set['k']
。
The elements of char_set
are boolean
s. char_set
的元素是boolean
s。 They are all initialized to the default value for that type ( false
), when you create the array.当您创建数组时,它们都被初始化为该类型的默认值 (
false
)。 The values of some of those elements may be tested in其中一些元素的值可以在
if (char_set[val]) //...
(yielding one of the two possible values of type boolean
, whichever was most recently set) and may be set to true
in (产生类型的两个可能值中的一个
boolean
,无论哪个是最近设置的),并可以被设置为true
在
char_set[val] = true;
in each case, val
is the index of the element involved, not the value to set or read.在每种情况下,
val
是所涉及元素的索引,而不是要设置或读取的值。
Note, however, that this code has at least two significant problems:但是请注意,这段代码至少有两个重大问题:
String.charAt()
returns values of type char
, whose numeric range goes up to 65535
. String.charAt()
返回char
类型的值,其数值范围可达65535
。 Therefore, the code presented can easily generate an ArrayIndexOutOfBoundsException
.因此,提供的代码可以轻松生成
ArrayIndexOutOfBoundsException
。
String
s are indexed starting from 0
, so the first character in the string is not included in the analysis. String
从0
开始索引,因此字符串中的第一个字符不包括在分析中。 In a general sense, this could be intentional, but in an "analyze this code" sense it is important to recognize.从一般意义上讲,这可能是有意为之,但在“分析此代码”的意义上,认识到这一点很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.