leetcode第四周解题报告
来源:互联网 发布:全国出生缺陷监测数据 编辑:程序博客网 时间:2024/06/06 05:28
7. Reverse Integer
题目描述:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
解题思路:
这一题是简单题,但是还是有两个细节需要处理:
第一,负溢出和正溢出的绝对值最大值不一样,为了减少代码,需要做一些对齐。
第二,判定溢出也是个问题(用float来判定Int是否溢出这种耍流氓的事情本人是不会做的。)
第一点很好解决,单独考虑INT_MIN,这样正负限的绝对值都为2147484647.
第二点则略微复杂,当输入的数字是10位数时,假设为k1k2k3...k10, 本人将其拆解为k1 * 10^9 + k2k3...kn,再将k1与INT_MAX的最高位做比较,k2....kn与INT_MAX的余下部分作比较,判断大小,从而判断溢出。
代码如下:
class Solution {public: int reverse(int x) { if (x == INT_MIN) return 0; int sign = (x > 0 ? 1 : -1); int xAbs = x / sign; int record(xAbs); int res(0); while(xAbs / 10 != 0){ res = res * 10 + xAbs % 10; xAbs /= 10; } if(res > INT_MAX / 10 || (res == INT_MAX / 10 && xAbs > INT_MAX % 10)){ return 0; } res = res * 10 + xAbs % 10; res *= sign; int sign1 = (res > 0 ? 1 : -1); return(sign1 == sign ? res : 0); }};运行结果如下:
66. Plus One
题目描述:
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
解题思路:本人的解题思路是,使用数组模拟大数加法操作即可。但是这样做有个缺陷,就是当最高位进位时,数组需要重新复制一遍,耗费时间,而看了答案后豁然开朗。答案的思路是:当最高位要进位时,等价于改变当前最高位为1并且在尾部加0.
代码如下:
class Solution {public: vector<int> plusOne(vector<int>& digits) { vector<int>res(digits.size() + 1); int carry = 1; for(int i = digits.size() - 1; i >= 0; --i){ res[i + 1] = (digits[i] + carry) % 10; carry = (digits[i] + carry) / 10; } res[0] += carry; if (res[0] > 0) return res; vector<int>res1(digits.size()); for(int i = 0; i < digits.size(); ++i){ res1[i] = res[i + 1]; } return(res1); }};
运行结果如下:
19. Palindrome Number
题目描述:
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
解题思路:简单题,用数学求模运算构建指向头和尾的向中间移动的指针即可。
代码如下:
class Solution {public: bool isPalindrome(int x) { if(x == INT_MIN)return false; if(x < 0) return false; int bits = 0; int tmp(x); while(0 != tmp / 10){ ++bits; tmp /= 10; } int mostS = 1;; for(int i = 0; i < bits; ++i){ mostS *= 10; } int remBits = bits + 1; while (remBits > 1){ int left = x / mostS; int right = x % 10; if(left != right) return false; x = x % mostS; x = (x - right) / 10; remBits -= 2; mostS /= 100; } return true; }};结果如下:
- leetcode第四周解题报告
- 算法第一周LeetCode解题报告
- leetcode第四周解题总结(9,21,2,24)
- 算法第十四周解题报告
- 第四周实验报告
- 第四周实验报告
- LeetCode OJ 第1题 Two Sum 解题报告
- LeetCode OJ 第2题 Add Two Numbers 解题报告
- Leetcode 400. Nth Digit 第n个数字 解题报告
- LeetCode 解题报告索引
- [LeetCode]Permutations,解题报告
- Leetcode Triangle 解题报告
- [LeetCode]Anagrams,解题报告
- leetCode解题报告
- [LeetCode]Candy, 解题报告
- 【LeetCode】Candy 解题报告
- 【LeetCode】Triangle 解题报告
- 【LeetCode】Permutations 解题报告
- 子类重写父类方法条件
- 主动信息收集之端口扫描
- 使用PowerDesigner连接数据库并生成ER图
- 生成RSA秘钥及加密
- Name [spring.liveBeansView.mbeanDomain] is not bound in this Context. Unable to find
- leetcode第四周解题报告
- NuGet学习笔记(1)——初识NuGet及快速安装使用
- linux 安装软件常用中终端命令
- L1-020. 帅到没朋友
- leetcode_83. Remove Duplicates from Sorted List
- android LayoutInflaterCompat解析
- 二分——GDUT #C 进击的调查兵团
- www上传相关
- 写代码工具