简体   繁体   English

Java,mask [s.charAt(i)]是什么意思? mask是一个布尔数组

[英]Java, what does mask[s.charAt(i)] mean? mask is a boolean array

I just begin to do the problems in cracking the coding interview. 我只是开始做破解编码面试中的问题。 this is the first question: implement an algorithm to determine if a string has all unique characters. 这是第一个问题:实施一种算法来确定字符串是否具有所有唯一字符。 What if you cannot use additional data structures? 如果您不能使用其他数据结构怎么办? Here is the solution: Using a boolean array to track the occurrence of each possible character in ASCII set. 解决方案如下:使用布尔数组跟踪ASCII集中每个可能字符的出现。 Initially every value in that array is false, until the corresponding character value appears. 最初,该数组中的每个值都是false,直到出现相应的字符值为止。 If that character value appears again, then if the corresponding value in array is already true, return false. 如果该字符值再次出现,则如果数组中的相应值已经为true,则返回false。 Time: O(n), n is the length of string. 时间:O(n),n是字符串的长度。 Space: O(n), the array named mask. 空格:O(n),名为mask的数组。 Source Code: 源代码:

public class Interview {

    // Assume every character in string is in ASCII.
    public boolean uniqueChars(String s) {
        boolean[] mask = new boolean[256];
        for (int i = 0; i < s.length(); i++) {
            if (mask[s.charAt(i)])
                return false;
            mask[s.charAt(i)] = true;
        }
        return true;
    }
} 

I cannot understand this: mask[s.charAt(i)]. 我不明白这一点:mask [s.charAt(i)]。 I think s.charAt(i) is a char but not a number, so I am don't know how this works. 我认为s.charAt(i)是一个char而不是一个数字,所以我不知道它是如何工作的。 I know this is a simply question, but I cannot find a direct answer. 我知道这是一个简单的问题,但我找不到直接的答案。 Hope you guys can help me.Thanks a lot. 希望你们能帮助我。非常感谢。

mask[s.charAt(i)] gets the character from s at index i , converts it to an int (implicitly), and then looks up the value in mask at that index in the mask array. mask[s.charAt(i)]从索引i处的s获得字符,将其转换为int (隐式),然后在mask数组中的该索引处查找mask中的值。 The int value of the character is not ASCII, it's UTF-16 (because that's how Java's characters are defined ). 字符的int不是 ASCII,而是UTF-16(因为这是Java字符的定义方式 )。 The code makes a massive assumption (mentioned in a comment) that the string only has ASCII characters, which it apparently defines as characters whose value is < 256 ; 该代码做出了一个很大的假设(在注释中提到),该字符串仅包含ASCII字符,显然它定义为值< 256字符; that's "extended" ASCII, ASCII itself only defines characters < 128 . 这是“扩展的” ASCII,ASCII本身仅定义< 128字符。 For values < 128 , ASCII and UTF-16 are the same. 对于值< 128 ,ASCII和UTF-16相同。

Your overall function is doing this: Starting with an array with all false entries (because that's the default state of a boolean[] ), it loops through the string s and, for each character, looks up whether it's seen that character before. 您的总体功能是这样做的:从带有所有false条目的数组开始(因为这是boolean[]的默认状态),它循环遍历字符串s并针对每个字符查找是否之前已看到该字符。 If it has, return true out of the function (we've seen at least two occurrences of the character). 如果有,则从函数中返回true (我们已经看到至少两次出现该字符)。 If it hasn't seen a character before, it sets that entry in the array to true . 如果以前没有看过字符,则会将数组中的该条目设置为true If it gets all the way to the end of the string without returning true , it returns false — the string didn't have the same character repeated twice. 如果它一直返回到字符串的末尾而不返回true ,则返回false -字符串没有重复两次相同的字符。

The code will fail with an ArrayIndexOutOfBoundsException if it sees a character whose int value is >= 256 , which is entirely likely. 如果代码看到int>= 256的字符,则该代码将失败并返回ArrayIndexOutOfBoundsException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ++array[s.charAt(i) - &#39;A&#39;] 究竟做了什么? - What exactly does ++array[s.charAt(i) - 'A'] do? 如何写一个函数布尔成功(char a,char b,String s)? 使用s.equals(“”),s.charAt(0),s.substring(1) - How can I write a function boolean succeeds(char a, char b, String s)? using s.equals(“”), s.charAt(0), s.substring(1) whats (s.charAt(low) != s.charAt(high)) {&quot; and &quot; int high = s.length() - 1; - whats (s.charAt(low) != s.charAt(high)) {" and " int high = s.length() - 1; 从&#39;a&#39;||简化s.charAt &#39;b&#39;|| &#39;c&#39;|| 到一个s.charAt - Simplify s.charAt from 'a' || 'b' || 'c' || to one s.charAt 在使用“ addTextChangedListener”和“ Character.isLetterOrDigit(s.charAt(i))”收听时如何启用文本 - How to enable text when listening with “addTextChangedListener” and “Character.isLetterOrDigit(s.charAt(i))” 使用 String.valueOf(s.charAt(i)) 从 ArrayList 中删除 - Removing from ArrayList using String.valueOf(s.charAt(i)) 我正在尝试从定义的字符串中删除字符串中的字符。 错误出现在这里:if(s.charAt(x)== punct.charAt(y)) - I am trying to remove characters in a string from a defined string. the error comes here: if(s.charAt(x) == punct.charAt(y)) Java:如何创建使用掩码从字节数组中提取整数拆分位的方法 - Java: How do I create a Method to extract an Integer's split bits from a byte array using a mask 使用swing的MaskFormatter的电子邮件的掩码是什么 - What is the mask for emails using swing's MaskFormatter Java:SourceDataLine.write(...)是否屏蔽中断? - Java: Does SourceDataLine.write(…) mask Interrupts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM