当前位置:网站首页>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
}边栏推荐
- Distributed transaction -- middleware of TCC -- selection / comparison
- Investment demand and income forecast report of China's building ceramics industry, 2022-2028
- What is the Valentine's Day gift given by the operator to the product?
- Data mining vs Machine Learning: what is the difference between them? Which is more suitable for you to learn
- Is the securities account opened by Caicai for individuals safe? Is there a routine
- 国元证券开户是真的安全可靠吗
- Selenium library 4.5.0 keyword explanation (III)
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- [CSDN Q & A] experience and suggestions
- [complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
猜你喜欢

Is it really possible that the monthly salary is 3K and the monthly salary is 15K?

MySQL is installed as a Windows Service

Recommendation of knowledge base management system

BBS forum recommendation

(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/3f/75b3125f8779e6cf9467a30fd7eeb4.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!

Report on prospects and future investment recommendations of China's assisted reproductive industry, 2022-2028 Edition

What does redis do? Redis often practices grammar every day

JDBC Technology

MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
随机推荐
Advanced C language - pointer 2 - knowledge points sorting
Sorry, Tencent I also refused
Generic tips
Att & CK actual combat series - red team actual combat - V
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
STM32 GPIO CSDN creative punch in
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
What is the future of software testing industry? Listen to the test veterans' answers
(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
Suggestions for improving code quality
Pair
No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
Unity elementary case notes of angry birds Siki college 1-6
Stock price forecast
挖财帮个人开的证券账户安全吗?是不是有套路
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
[NLP] text classification still stays at Bert? Duality is too strong than learning framework
Gossip about redis source code 77
[about text classification trick] things you don't know
Make small tip