Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Convert String to Integer

Medium Acceptance 60.00% Points 30.00

You are given a string s that represents a valid non-negative integer. Your task is to convert the string into its corresponding integer value. You are not allowed to use built-in conversion functions such as stoi(), parseInt(), or similar methods. Return the integer value.

Examples
Example 1

Example 1

Input: s = "1234"

Output: 1234

Explanation: The string is converted to its integer value.

Example 2

Example 2

Input: s = "0010"

Output: 10

Explanation: Leading zeros are ignored in the integer value.

Example 3

Example 3

Input: s = "7"

Output: 7

Explanation: A single digit string converts directly to the same integer.

Hints
Hint 1
N/A
Constraints
  • 1 = length of s = 105
  • The string contains digits only (0–9)
Companies
Apple Samsung Media.net
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions