LeetCode学习-golang字符串转整数(atoi)(题8)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88390090

题面

请你来实现一个 atoi 函数,使其能将字符串转换成整数。
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
在任何情况下,若函数不能进行有效的转换时,请返回 0。
说明:
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [ 2 31 , 2 31 1 ] [−2^{31}, 2^{31} − 1] 。如果数值超过这个范围,返回 INT_MAX ( 2 31 1 ) (2^{31} − 1) 或INT_MIN ( 2 31 ) (−2^{31})

示例 1:
输入: “42”
输出: 42

示例 2:
输入: " -42"
输出: -42
解释: 第一个非空白字符为 ‘-’, 它是一个负号。
我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

示例 3:
输入: “4193 with words”
输出: 4193
解释: 转换截止于数字 ‘3’ ,因为它的下一个字符不为数字。

示例 4:
输入: “words and 987”
输出: 0
解释: 第一个非空字符是 ‘w’, 但它不是数字或正、负号。
因此无法执行有效的转换。

示例 5:
输入: “-91283472332”
输出: -2147483648
解释: 数字 “-91283472332” 超过 32 位有符号整数范围。
因此返回 INT_MIN ( 2 31 ) (−2^{31})

源码

package myAtoi
func MyAtoi(str string) (result int) {
	if len(str)>=0 {
		sign:=false
		for i:=0;i<len(str);i++ {
			temp:=str[i:i+1]
			if temp != " " {
				temp1:=[]byte(temp)
				if i==0 && (temp1[0] == '+' || temp1[0] == '-'){
					if temp1[0] == '+' {

					}else if temp1[0] == '-' {
						sign = true
					}
				}else {
					if temp1[0] >= '0' && temp1[0] <= '9' {
						result = result*10 + int(temp1[0] - '0')
					} else {
						break
					}
				}
			}
		}
		if sign {
			result = -result
		}
		if result <= (-1 << 31) {
			result = -1 << 31
		}
		if result >= (1<<31 -1){
			result = 1<<31 -1
		}
	}
	return
}

测试代码

package test

import (
	"fmt"
	"github.com/glw0223/LeetCode-go/myAtoi"
	"testing"
)

func TestMyAtoi(t *testing.T){
	var result int
	result=myAtoi.MyAtoi("42")
	fmt.Println(result)
	result=myAtoi.MyAtoi("-42")
	fmt.Println(result)
	result=myAtoi.MyAtoi("4193 with words")
	fmt.Println(result)
	result=myAtoi.MyAtoi("-91283472332")
	fmt.Println(result)
}

测试结果

API server listening at: 127.0.0.1:54515
=== RUN   TestMyAtoi
42
-42
4193
-2147483648
--- PASS: TestMyAtoi (0.00s)
PASS

Debugger finished with exit code 0

复杂度

时间复杂度:O(n)
空间复杂度:O(1)

猜你喜欢

转载自blog.csdn.net/glw0223/article/details/88390090