leecode之aoti
来源:互联网 发布:怪物猎人ol数据互通 编辑:程序博客网 时间:2024/06/06 09:50
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
最近一段时间,发现自己编写代码的能力下降了很多。所以,网上找了一下,看看有没有类似可以提高的项目。偶尔看别人都在leetcode上刷题目,感觉特别有意思,所以拿来练练手。因为leetcode上每个题目有很多测试用例,所以你上传的代码必须要全部通过这些测试用例才行。
不多说,为了不丢人。先找容易的题目开始,这样也好尽快上手。atoi,其实就是字符串转变为整数的代码。看着容易,其实也不容易,因为你需要考虑各种情况,其实这对我们提高代码质量是很有帮助的。代码你可以根据自己的需要写成cpp、java或者python的格式。
因为我的机器上没有安装vc编译器,所以只能根据网站反馈的信息进行调整,看日志前后花了快1个半小时,工作了这么多年,这个成绩还是很丢人的。大家可以根据自己的需要到leetcode上面试一试,权当练习好了。看到这份博客的同学最好先不要看代码,自己去试一试,看看能不能在保证正确的时候保证性能的最大化。
为了不侵犯原作者的版权,这边只是给出我个人对题目的理解和答案,当然仅仅是抛砖引玉了。
#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;}};
2 0
- leecode之aoti
- itoa/aoti
- leecode之3Sum
- leecode 之 longestcommonprefix
- leecode
- LeeCode
- aoti重新写
- atof & aoti 函数实现
- 实现aoti() 函数
- leecode 1
- leecode 2
- leecode 3
- leecode 6
- leecode:combinations
- Leecode刷题
- leecode 357
- Leecode Week1
- LeeCode Week2
- Java对象的序列化与反序列化
- HDU2547 无剑无我【水题】
- sublime 安装ctags
- c++ standard library fill_n demo
- 【C#】——面向对象思想
- leecode之aoti
- 静态变量,静态局部变量的生存周期
- 字符串-04. 字符串逆序(15)
- ubuntu安装r-base install.package("rJava") 问题
- CSS之渐变效果
- htto://www.icsmj8.com http://www.suhe0.com http://www.efaaa.com 佳佳影院 http://www.7sykj.com 东阳网站建设
- Android Parcelable理解与使用(对象序列化)
- linux c编程 教程汇总
- 蓝桥杯 算法训练 安慰奶牛 最小生成树