7 - Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Notice:

1.If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

2.Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

3.For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

  1. int reverse(int x)
  2. {
  3. long long num=x; //To avoid overflow
  4. long long result=;
  5. int sign=;
  6. if(num<){
  7. sign=-;
  8. num=-num;
  9. }
  10. while(num!=){
  11. result=result*+num%;
  12. num/=;
  13. }
  14. if(result>INT_MAX){
  15. if(result==INT_MAX+&&sign==-)return INT_MIN;
  16. else
  17. return ;
  18. }
  19. return result*sign;
  20. }

8 - String to Integer

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Tips:

1. Input:"+-2" Output:-2 Expected:0

2. Each time input str[i], it needs to judge if overflow happened, the number may exceed either long or long long

  1. int myAtoi(string str)
  2. {
  3. if(str.size()==)return ;
  4. int i=,flag=;
  5. while(str[i]==' ')i++;
  6. if(str[i]=='-'||str[i]=='+')
  7. {
  8. if(str[i]=='-')flag=-;
  9. i++;
  10. }
  11. if(str[i]>''||str[i]<'')return ;
  12. long long temp;
  13. for(int j=i;j<str.size();j++)
  14. {
  15. if(str[j]>''||str[j]<'')break;
  16. temp=temp*+(str[j]-'');
  17. if(flag== && temp>INT_MAX)
  18. return INT_MAX;
  19. else if(flag==- && temp>INT_MAX)
  20. return INT_MIN;
  21. }
  22. return (int)(temp*flag);
  23. }

【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits

    190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  5. 【LeetCode】758. Bold Words in String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  7. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  8. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

随机推荐

  1. C语言ASM汇编内嵌语法【转】

    转自:http://www.cnblogs.com/latifrons/archive/2009/09/17/1568198.html GCC 支持在C/C++代码中嵌入汇编代码,这些汇编代码被称作G ...

  2. jsp中@import导入外部样式表与link链入外部样式表的区别

    昨天碰到同事问了一个问题,@impor导入外部样式与link链入外部样式的优先级是怎样的,为什么实验的结果是按照样式表导入后的位置来决定优先级.今天就这个问题具体总结如下:   先解释一下网页添加cs ...

  3. HeadFirst Jsp 04 (请求和响应作为servlet)

    servlet 的存在就是为了客服服务, servlet的任务是得到一个客户的请求, 再发回一个响应. 由上图可知, web 容器会在启动后就加载所有的servlet类, 并为之创建实例和初始化 注意 ...

  4. 【Todo】Kerberos、OpenSSL、OAuth2.0等一系列验证权限安全相关学习

    Kerberos可以先从这一篇开始: http://idior.cnblogs.com/archive/2006/03/20/354027.html

  5. D:/apache2/conf/httpd.conf:Cannot load D:/apache2/modules/mod_actions.so

    报错如下: errors reported here must be corrected before service can be started.httpd:Syntax error on lin ...

  6. spring事务传播属性与隔离级别

    一.Propagation (事务的传播属性) Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为. 有以下选项可供使用: PROPAGATIO ...

  7. UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)

    题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...

  8. Linux kernel scriptes bin2c "\x"

    /**************************************************************************** * Linux kernel scripte ...

  9. 使用NPOI创建Excel文件

    Public Sub BuildExcel() '写入内容到Excel Dim hssfworkbook As HSSFWorkbook = WriteExcel() Dim destFileName ...

  10. dede 字符串截取

    [field:description function="( strlen(strip_tags('@me',''))>100 ? cn_substr(strip_tags('@me' ...