Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode-8. String to Integer (atoi) #15

Closed
ninehills opened this issue Jul 19, 2017 · 0 comments
Closed

LeetCode-8. String to Integer (atoi) #15

ninehills opened this issue Jul 19, 2017 · 0 comments
Labels

Comments

@ninehills
Copy link
Owner

ninehills commented Jul 19, 2017

20170719

问题

https://leetcode.com/problems/string-to-integer-atoi/#/description

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

spoilers alert... click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

思路

难点是边界处理,转整数的逻辑就是循环乘以10即可

解答

package main

import "fmt"

// ----------------------

const MaxInt = int32(^uint32(0) >> 1)
const MinInt = -MaxInt - 1

func myAtoi(str string) int {
	var r int64 = 0
	var tmp_r int64 = 0
	var flag int64 = 1
	var head bool = true
	for _, char := range str {
		if head {
			if char == ' ' {
				continue
			}
			if char == '-' {
				flag = -1
				head = false
				continue
			}
			if char == '+' {
				flag = 1
				head = false
				continue
			}
		}
		if char >= '0' && char <= '9' {
			head = false
			r = r*10 + (int64(char) - int64('0'))
			tmp_r = r * flag
			if tmp_r > int64(MaxInt) {
				return int(MaxInt)
			}
			if tmp_r < int64(MinInt) {
				return int(MinInt)
			}
		} else if head == false {
			break
		} else {
			return 0
		}
	}
	return int(r)
}

// ----------------------

func main() {
	fmt.Println(myAtoi("123123123123123"))
	fmt.Println(myAtoi("9223372036854775809"))
	fmt.Println(myAtoi("-123"))
	fmt.Println(myAtoi("43521"))
	fmt.Println(myAtoi("    010"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant