81145

Questions:Implement an algorithm to determine if a string has all unique characters. What if you can

Question:

solution:

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

I have some questions:

<ol><li>

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?

</li> <li>

int val=str.charAt(i) I searched ASCII table, <img alt="ASCII table" class="b-lazy" data-src="https://i.stack.imgur.com/jiOi5.png" data-original="https://i.stack.imgur.com/jiOi5.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

</li> </ol>

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

<ol start="3"><li>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?</li> </ol>

Answer1:

From the <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" rel="nofollow">docs</a>:

<blockquote>

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

</blockquote>

To make your solution work for Unicode characters too, change these lines:

<blockquote> if (str.length() > 256) return false; boolean[] char_set = new boolean[256]; </blockquote>

To this instead:

if (str.length() > Character.MAX_VALUE + 1) return false; boolean[] char_set = new boolean[Character.MAX_VALUE + 1]; <hr /><blockquote> <ol start="2"><li>int val=str.charAt(i) I searched ASCII table. If the string is "@ABCD", what number will return? (which of the three "Dec, Hx, Oct" will be use?)</li> </ol></blockquote>

You could just run the code and see for yourself. "@ABCD".charAt(0) returns @ of type char. Converted to int that's 64 as per the ASCII table.

Dec, Hx, Oct are all just translations of the same thing. Just like "book" in Spanish will be "libro", 63 in hexadecimal will be 0x3f.

<blockquote> <ol start="3"><li>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?</li> </ol></blockquote>

I guess this is how char is defined in the language itself, but I can't find the authoritative reference.

Answer2:

<blockquote>

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?

</blockquote>

When you write int i = str.charAt(...), the Java language spec calls that <em>Widening Primitive Conversion</em>. 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.

<a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#25214" rel="nofollow">http://docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html#25214</a>

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.

<hr />

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

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 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.

<a href="http://en.wikipedia.org/wiki/Hexadecimal" rel="nofollow">http://en.wikipedia.org/wiki/Hexadecimal</a>

<a href="http://en.wikipedia.org/wiki/Octal" rel="nofollow">http://en.wikipedia.org/wiki/Octal</a>

Recommend

  • TCL procedure call with optional parameters
  • How to remove \\u0000 from string?
  • Scala - Converting from ISO-8859-1 to UTF-8 gives foreign character strangeness
  • raise ValueError('Series lengths must match to compare') while manipulating dataframe
  • SSRS display top n but have SUM include all values
  • no difference between [..] and […] for array?
  • How to Implement Touch Up Inside in touchesBegan, touchesEnded
  • Java Spring Jackson json library returning unicode instead of text
  • How to generate Date Series in HIVE? (Creating table)
  • How to negative match regex in JavaScript string replace? [duplicate]
  • Python: “Print” and “Input” in one line [duplicate]
  • How java deals with references to a generic type
  • TCPDF's getNumLines() is sometimes wrong
  • Why does NotifyIcon not set SynchronizationContext?
  • Exporting SAS DataSet on to UNIX as a text file…with delimiter '~|~'
  • Input buffer flush
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Show records ordered with maximum price first in PHP & MySQL
  • Mongo JSON document -> JSON -> BSON
  • how to check the textarea content is blank using javascript?
  • How to initialize context? [closed]
  • Using HTML/CSS for UI in XNA?
  • How to configure Cygnus in relation to Orion and Cosmos
  • C function strchr - How to calculate the position of the character?
  • Salesforce Different WSDL files and when to use
  • Find group of records that match multiple values
  • Trying to get the char code of ENTER key
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Window Size for Mac application
  • Redux, normalised entities and lodash merge
  • How do you troubleshoot character encoding problems?
  • Do create extension work in single-user mode in postgres?
  • Linker errors when using intrinsic function via function pointer
  • A cron job substitute?
  • LevelDB C iterator
  • Django query for large number of relationships