当前位置:网站首页>8. Go implementation of string conversion integer (ATOI) and leetcode
8. Go implementation of string conversion integer (ATOI) and leetcode
2022-07-04 00:26:00 【Li Jue】
8. String conversion integers (atoi)
Please come to realize a myAtoi(string s) function , Enable it to convert a string to a 32 Bit signed integer ( similar C/C++ Medium atoi function ).
function myAtoi(string s) The algorithm is as follows :
Read in strings and discard useless leading spaces
Check the next character ( Suppose you haven't reached the end of the character yet ) Positive or negative , Read the character ( If there is ). Determine whether the final result is negative or positive . If neither exists , Suppose the result is positive .
Read in the next character , Until you reach the next non numeric character or the end of the input . The rest of the string will be ignored .
Convert the numbers read in the previous steps into integers ( namely ,"123" -> 123, "0032" -> 32). If you don't read in the numbers , Then the integer is 0 . Change the symbol if necessary ( From step 2 Start ).
If the number of integers exceeds 32 Bit signed integer range [−231, 231 − 1] , You need to truncate this integer , Keep it in this range . say concretely , Less than −231 The integer of should be fixed to −231 , Greater than 231 − 1 The integer of should be fixed to 231 − 1 .
Returns an integer as the final result .
Be careful :
The white space character in this question only includes the space character ' ' .
Except for the leading space or the rest of the string after the number , Do not ignore Any other character .
Example 1:
Input :s = "42"
Output :42
explain : The bold string is the character that has been read in , The caret is the character currently read .
The first 1 Step :"42"( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :"42"( No characters are currently read in , Because it doesn't exist here '-' perhaps '+')
^
The first 3 Step :"42"( Read in "42")
^
Parse to get an integer 42 .
because "42" In scope [-231, 231 - 1] Inside , The final result is 42 .
Example 2:
Input :s = " -42"
Output :-42
explain :
The first 1 Step :" -42"( Read in leading space , But ignore )
^
The first 2 Step :" -42"( Read in '-' character , So the result should be negative )
^
The first 3 Step :" -42"( Read in "42")
^
Parse to get an integer -42 .
because "-42" In scope [-231, 231 - 1] Inside , The final result is -42 .
Example 3:
Input :s = "4193 with words"
Output :4193
explain :
The first 1 Step :"4193 with words"( No characters are currently read in , Because there are no leading spaces )
^
The first 2 Step :"4193 with words"( No characters are currently read in , Because it doesn't exist here '-' perhaps '+')
^
The first 3 Step :"4193 with words"( Read in "4193"; Because the next character is not a number , So read in stop )
^
Parse to get an integer 4193 .
because "4193" In scope [-231, 231 - 1] Inside , The final result is 4193 .
Tips :
0 <= s.length <= 200
s By the English letters ( Uppercase and lowercase )、 Numbers (0-9)、' '、'+'、'-' and '.' form
Traverse : Simulation is enough
func myAtoi(s string) int {
// Traverse
s = strings.TrimSpace(s)
result := 0
flag := 1
for i, v := range s {
if v >= '0' && v <= '9' {
result = result*10 + int(v-'0')
} else if v == '-' && i == 0 {
flag = -1
} else if v == '+' && i == 0 {
flag = 1
} else {
break
}
if result > math.MaxInt32 {
if flag == -1 {
return math.MinInt32
}
return math.MaxInt32
}
}
return flag * result
}
边栏推荐
- JDBC Technology
- How to make recv have a little temper?
- Reading notes on how programs run
- [MySQL] classification of multi table queries
- Social network analysis -social network analysis
- Global and Chinese markets for instant saliva testing devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
- [source code] VB6 chat robot
- MySQL is installed as a Windows Service
- Solve the problem that the kaggle account registration does not display the verification code
猜你喜欢
Eight year test old bird, some suggestions for 1-3 year programmers
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
Collation of the most complete Chinese naturallanguageprocessing data sets, platforms and tools
Pytorch learning notes 5: model creation
Analysis: misunderstanding of choosing WMS warehouse management system
[2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
leetcode-43. String multiplication
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Vscode regular match replace console log(.*)
随机推荐
Alibaba cloud container service differentiation SLO hybrid technology practice
Selenium library 4.5.0 keyword explanation (III)
Global and Chinese market of process beer equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Makefile judge custom variables
Iclr2022: how does AI recognize "things I haven't seen"?
Regular expressions and text processors for shell programming
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
Speed up the energy Internet of things. What can low-power Internet of things technology represented by Zeta do?
Idea a method for starting multiple instances of a service
Eight year test old bird, some suggestions for 1-3 year programmers
Optimization of for loop
Smart fan system based on stm32f407
Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition
Distributed transaction -- middleware of TCC -- selection / comparison
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
SPI based on firmware library
Social network analysis -social network analysis
[cloud native topic -48]:kubesphere cloud Governance - operation - overview of multi tenant concept