20200403-leetcode-剑指offer-面试题67 把字符串转换成整数

20200403-leetcode-剑指offer-面试题67 把字符串转换成整数_第1张图片
题目链接:

开始用C语言写的太麻烦了,python版本如下

python下面有个strip方法,可以去除字符串开头和结尾的指定字符

20200403-leetcode-剑指offer-面试题67 把字符串转换成整数_第2张图片

class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """

        ans = 0
        book = 1

        # 去掉左右两端的空格
        str = str.strip()

        if len(str)==0:
            return 0

        if str[0] == '-' or str[0] == '+':
            if str[0] == '-':
                book = -1
            for i in range(1, len(str)):
                if '0' <= str[i] <= '9':
                    ans = ans * 10 + int(str[i])
                else:
                    break
        elif '0' <= str[0] <= '9':
            for i in range(len(str)):
                if '0' <= str[i] <= '9':
                    ans = ans * 10 + int(str[i])
                else:
                    break
        else:
            return 0

        ans = ans * book
        if -1 * (2 ** 31) <= ans <= (2 ** 31 - 1):
            return ans
        elif ans < 0:
            return -1 * (2 ** 31)
        else:
            return (2 ** 31 - 1)

C语言版本如下:

class Solution {
public:

	bool check(char s) {
		if (s == ' ' || s == '+' || s == '-' || (s >= '0' && s <= '9')) return true;
		return false;
	}
	//处理0-9
	long long solveNumber(string s, int begin, int book)
	{
		int end = begin;
		while (s[end] >= '0' && s[end] <= '9') { end++; }
		end--;
		long long res = 0;
		for (int i = begin; i <= end; i++)
		{
			if (res >= INT_MAX) {
				if (book == 1) return INT_MAX;
				else return 2147483648;
			}
			res = res * 10 + (s[i] - '0');
		}
		return res;
	}

	int strToInt(string str) {
		int len = str.length();
		if (len == 0) return 0;                //字符串是一个空字符
		if (!check(str[0])) return 0;        //字符开头不是合法字母
		int pos = 0;
		long long res = 0;
		if (str[0] == ' ')
		{
			while (str[pos] == ' ') { pos++; }
			if (pos >= len) return 0;

			if (str[pos] == '+') {
				pos++;
				if (pos >= len) return 0;

				if (str[pos] >= '0' && str[pos] <= '9') res = solveNumber(str, pos, 1);
				else res = 0;
			}
			else if (str[pos] == '-') {
				pos++;
				if (pos >= len) return 0;

				if (str[pos] >= '0' && str[pos] <= '9') { res = 0 - solveNumber(str, pos, -1); }
				else res = 0;
			}
			else if (str[pos] >= '0' && str[pos] <= '9') {
				res = solveNumber(str, pos, 1);
			}
		}
		else if (str[0] == '+')
		{
			pos++;
			if (pos >= len) return 0;

			if (str[pos] >= '0' && str[pos] <= '9') res = solveNumber(str, pos, 1);
			else res = 0;
		}
		else if (str[0] == '-')
		{
			pos++;
			if (pos >= len) return 0;

			if (str[pos] >= '0' && str[pos] <= '9') {
				res = 0 - solveNumber(str, pos, -1);
			}
			else res = 0;
		}
		else if (str[0] >= '0' && str[0] <= '9')
		{
			res = solveNumber(str, 0, 1);           //字符串s,起始为数字0-9的位置0
		}
		if (res >= 2147483647) return INT_MAX;
		else if (res < -2147483647) return INT_MIN;
		else { return res; }

	}
};

你可能感兴趣的:(leetcode)