📣 独立日限时特惠: VIP通行证立减$68
查看: 4865| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] leetcode: Valid Number

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
https://oj.leetcode.com/problems/valid-number/
今晚编了一编。编程不好,努力提高中。
系统说:
Last executed input:                " "
我咋感觉我的代码挺对的,不会有这个bug呢?
我的代码:
  1. class Solution {
  2. public:
  3.     bool isNumber(const char *s) {
  4.         string str = s;
  5.         int start = 0;
  6.         int end = str.length() - 1;
  7.         char tmp;
  8.         
  9.         // string.trim
  10.         tmp = str.at(start);
  11.         while((start <= end) && (tmp == ' ' || tmp == '\n' || tmp == '\t' || tmp == '\r')){
  12.             start++;
  13.             tmp = str.at(start);
  14.         }   
  15.         if(start > end){
  16.             return false;
  17.         }
  18.         tmp = str.at(end);
  19.         while((start <= end) && (tmp == ' ' || tmp == '\n' || tmp == '\t' || tmp == '\r')){
  20.             end--;
  21.             tmp = str.at(end);
  22.         }   
  23.         
  24.         
  25.         if(str.at(start) == '+' || str.at(start) == '-'){
  26.             start++;
  27.         }
  28.         bool num = false; // is a digit
  29.         bool dot = false; // is a '.'
  30.         bool e = false;   // is a 'e'
  31.         
  32.         while(start <= end){
  33.             tmp = str.at(start);
  34.             if(tmp <= '9' && tmp >= '0'){
  35.                 num = true;
  36.             }
  37.             else if(tmp == '.'){
  38.                 if(dot || e){
  39.                     return false;
  40.                 }
  41.                 dot = true;
  42.             }
  43.             else if(tmp == 'e'){
  44.                 if(e || !num){
  45.                     return false;
  46.                 }
  47.                 e = true;
  48.                 num = false;
  49.             }
  50.             else if (tmp == '+' || tmp == '-') {
  51.                 if (str.at(start - 1) != 'e')
  52.                     return false;
  53.             }
  54.             else{
  55.                 return false;
  56.             }
  57.             start++;
  58.         }
  59.         return num;
  60.     }
  61. };
复制代码
求各位大神百忙之中指点一下啊!555~




上一篇:请教一下大家strStr()的问题
下一篇:Leetcode C++
🔗
fangl086 2014-10-30 10:15:27 | 只看该作者
全局:
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at
Aborted
http://www.cplusplus.com/reference/string/string/at/
The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not.
s="", len = 0, pos超出范围了,抛出异常。

评分

参与人数 1大米 +10 收起 理由
415044809 + 10 谢谢你的介绍!

查看全部评分

回复

使用道具 举报

🔗
fangl086 2014-10-30 10:16:05 | 只看该作者
全局:
  1. class Solution {
  2. public:
  3.         bool isNumber(const char *s) {
  4.                 int start = 0, end = strlen(s)-1;
  5.                 bool dot, e, num;
  6.                 dot = e = num = false;
  7.                 for (; start<strlen(s); start++) {
  8.                         if (s[start] != ' ') break;
  9.                 }
  10.                 for (; end>=0; end--) {
  11.                         if (s[end] != ' ') break;
  12.                 }
  13.                 if (s[start]=='+' || s[start]=='-') start++;
  14.                 if (start > end) return false;
  15.                 for (int i=start; i<=end; i++) {
  16.                         if ('0'<=s[i] && s[i]<='9') {
  17.                                 num = true;
  18.                         }
  19.                         else if (s[i]=='e' || s[i]=='E') {
  20.                                 if (e || !num || i==end) return false;
  21.                                 e = true;
  22.                         }
  23.                         else if (s[i] == '.') {
  24.                                 if (dot || e) return false;
  25.                                 if (!num && i==end) return false;
  26.                                 dot = true;       
  27.                         }
  28.                         else if (s[i]=='+' || s[i]=='-') {
  29.                                 if (i==end) return false;
  30.                                 if (s[i-1]!='e' && s[i-1]!='E') return false;
  31.                         }
  32.                         else return false;
  33.                 }
  34.                 return true;
  35.         }
  36. };
复制代码

评分

参与人数 2大米 +20 收起 理由
Tsien + 10 回答的很好!
415044809 + 10 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 415044809 2014-10-30 10:42:49 | 只看该作者
全局:
fangl086 发表于 2014-10-30 10:15
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at
Abo ...

晕,这么回事儿,太谢谢啦!以后用string.at()还得小心点。
其实那我就想写个string的trim函数,结果没写好。
太谢谢啦!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表