Leecode aoti, leecodeaoti

Source: Internet
Author: User

Leecode aoti, leecodeaoti


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


Recently, I found that my ability to write code is greatly reduced. So, I checked online to see if there are any projects that can be improved. It is particularly interesting to see other people brush questions on leetcode. Because each question on leetcode has many test cases, all the code you upload must pass these test cases.


Not to mention, to avoid shame. Start with an easy question. Atoi is actually the code for converting a string into an integer. It is easy to look at, but it is not easy, because you need to consider various situations, in fact, this is very helpful for us to improve the quality of code. You can write the code in cpp, java, or python format as needed.


Because the vc compiler is not installed on my machine, I can only adjust it based on the feedback from the website. It took nearly one and a half hours to read the logs. I have been working for so many years, this score is still very shameful. You can try the leetcode according to your own needs. If you see this blog, it is best not to read the code first. You can try it yourself to see if you can maximize the performance while ensuring the correctness.


In order not to infringe on the copyright of the original author, I am only here to give my personal understanding and answers to the question. Of course, it is just a reference.

#inlude <iostream>using namespace std;class Solution {public:static int atoi(const char* str) {int index = 0;int neg = 0;int total = 0;int len = strlen(str);char val  = 0;int count = 0;if(!len) return total;while(str[index] == ' ')index ++;if(str[index] == '+') index ++;else if(str[index] == '-') {neg = 1;index++;}for(; index < len; index ++) {val = str[index] - '0';count ++;if(val < 0 || val > 9)break;if(count > 10) {if(neg) total = 0x80000000;else total = 0x7fffffff;goto end;}if(!neg && count == 10) {if(val > (0x7fffffff - total * 10)) {total = 0x7fffffff;goto end;}}else if(neg && count == 10){if((unsigned int)val > (unsigned int)((-total * 10) - (int)(0x80000000))) {total = 0x80000000;goto end;}}total = total * 10 + val;}if (neg) total = -total;end:return total;}};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.