Trains the technique from
LeetCode 8String to Integer (atoi)This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A telemetry importer receives one text field per row and has to salvage a whole number from the front of it. Implement the salvage routine, which behaves like this:
field.+ or -, consume that one character and let it decide the sign. Only the first such character counts; a second one is not part of a number.0.-2147483648 through 2147483647 inclusive. A salvaged value below that range is reported as -2147483648, and a value above it as 2147483647.Return the value the importer stores. Do not treat any character other than a space as padding.
Example 1
Two spaces are stepped over, the minus fixes the sign, then 0073 is consumed and abc is discarded.
Example 2
The digit run ends at x, so the trailing 7 never gets looked at.
Example 3
A period is neither a space nor a sign nor a digit, so the cursor stops before any digit is consumed.
Example 4
The salvaged value sits below the register floor and is reported as the floor.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def salvage_number(field: str) -> int:public int salvageNumber(String field)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.