Using built-in function to check if the character ia digit in java, c#, c, c++, golang, python and php

2023-07-01 19:59:33

using built-in function to check if the character is a digit in java, c#, c, c++, golang, python and php.

Built-in function to check if a character is a digit

/* No built-in function, but could use the following 2 ways instead: */
/\d/.test(c) // regExp
c >= '0' && c<= '9' //Other programming languages could be used too.
unicode.IsDigit(rune(c)); // variable c is byte type
ctype_isdigit(c); // only [0-9] is supported
is_numeric(c); // also support 1.2, -1, 1e10


604. Design Compressed String Iterator

Design and implement a data structure for a compressed string iterator. The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.
Implement the StringIterator class:
next() Returns the next character if the original string still has uncompressed characters, otherwise returns a white space.
hasNext() Returns true if there is any letter needs to be uncompressed in the original string, otherwise returns false.
Example 1:
["StringIterator", "next", "next", "next", "next", "next", "next", "hasNext", "next", "hasNext"]
[["L1e2t1C1o1d1e1"], [], [], [], [], [], [], [], [], []]
[null, "L", "e", "e", "t", "C", "o", true, "d", true]
StringIterator stringIterator = new StringIterator("L1e2t1C1o1d1e1");; // return "L"; // return "e"; // return "e"; // return "t"; // return "C"; // return "o"
stringIterator.hasNext(); // return True; // return "d"
stringIterator.hasNext(); // return True
1 <= compressedString.length <= 1000
compressedString consists of lower-case an upper-case English letters and digits.
The number of a single character repetitions in compressedString is in the range [1, 10^9]
At most 100 calls will be made to next and hasNext.