Medium · Math

Convert string to integer

Given a string s, return the 32-bit signed integer it begins with: skip leading spaces, read at most one '+' or '-', read digits up to the first non-digit, and clamp the value to [−2³¹, 2³¹ − 1]; return 0 if no digit is read.

Examples

Example 1

{
  "s": "   -042"
}

Output: -42

Example 2

{
  "s": "91283472332"
}

Output: 2147483647

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Math problems