标签:index 1079 strS ret int atoi str Integer leetcode
分 空白 无符号 正号 负号 无意义符号分别讨论:
有点难看 应该写成函数分开的
Submission Detail
1079 / 1079 test cases passed. | Status:
Accepted |
Runtime: 44 ms Memory Usage: 13.3 MB | Submitted: 1 minute ago |
class Solution: def myAtoi(self, str: str) -> int: # 00 -(+) nums strS = len(str) # deal 0 if strS == 0: # str is empty return 0 #noraml maxnum = 0x80000000 for index in range(strS): if str[index] == ' ': # whitespace character continue else: # min if str[index] == '-': if index + 1 < strS: ret = '' for i in range(index + 1, strS): if str[i].isdigit(): ret = ret + str[i] else: break if ret == '-' or len(ret) == 0: return 0 if int(ret) <= maxnum: return int('-' + ret) else: ret =0 ret = ret - maxnum return int(ret) #plus if str[index] == '+': if index + 1 < strS: ret = '' for i in range(index + 1, strS): if str[i].isdigit(): ret = ret + str[i] else: break if ret == '+' or len(ret) == 0: return 0 if int(ret) <= maxnum: return int(ret) else: ret =0 ret = maxnum -1 return int(ret) # nosign or nomean if (str[index].isdigit()): ret = '' for i in range(index, strS): if str[i].isdigit(): ret = ret + str[i] else: break if (int(ret)<=maxnum-1): return int(ret) else: ret = maxnum - 1 return int(ret) else: return 0 #only one blankchar return 0
标签:index,1079,strS,ret,int,atoi,str,Integer,leetcode 来源: https://www.cnblogs.com/alfredsun/p/10804801.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。