f*******n 发帖数: 12623 | 1 其实,根据标准,结果是undefined,因为2147483648无法用32-bit integer代表。
一般的库给出来的结果是-2147483648,因为在32-bit integer里面,0-(-2147483648)
= -2147483648。 |
|
e****d 发帖数: 333 | 2 c++
abs((int)-2147483648)=-2147483648
这个如何理解? |
|
i**********e 发帖数: 1145 | 3 基本思路对,但可能有些 case 考虑不全,例如 INT_MIN = -2147483648,不是 -
2147483647(考虑 int 为 32-bit),要小心处理这个溢出,很多人很容易掉进的陷阱。
还有就是 "1a",应该 output 是 1,而不是 0。
不过我觉得这些细节不大要紧,最重要是问好问题来,知道 input requirement就好了。
可以在这里执行你的代码:
http://www.leetcode.com/onlinejudge
Problem: String to Integer (atoi)
Progress: 23/30 test cases passed.
input output expected
"-2147483648" 2147483647 -2147483648
"-2147483649" 2147483647 -2147483648 |
|
m****0 发帖数: 2236 | 4 ~ $ more test.cpp
#include
#include
#include
using namespace std;
int main(void){
int x = -2147483648;
cout<<"x="<
}
~ $ g++ -o test.o test.cpp
~ $ ./test.o
x=-2147483648 -x=-2147483648 abs(x)=-2147483648
~ $ |
|
w**z 发帖数: 8232 | 5 我再贴一下Java的sourcecode。不懂为什么要把正数转成负数再查溢出呢?有什么好处?
code里有Author的comments
// Accumulating negatively avoids surprises near MAX_VALUE
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sig... 阅读全帖 |
|
w**z 发帖数: 8232 | 6 Source Code from Java
/*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @version 1.92, 04/07/06
* @since JDK1.0
*/
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* A... 阅读全帖 |
|
i**********e 发帖数: 1145 | 7 Just tried running your code here:
http://www.leetcode.com/onlinejudge
it seemed it failed for some test cases when a or b == -2147483648.
This is because if (a < 0) a = -a; cause it to overflow for negative
integers. Since Java doesn't have unsigned int, you may have to use long
long to avoid overflow.
Run Status: Wrong Answer
Progress: 13/17 test cases passed.
input output expected
-2147483648, 1 0 -2147483648 |
|
i**********e 发帖数: 1145 | 8 Just tried running your code here:
http://www.leetcode.com/onlinejudge
it seemed it failed for some test cases when a or b == -2147483648.
This is because if (a < 0) a = -a; cause it to overflow for negative
integers. Since Java doesn't have unsigned int, you may have to use long
long to avoid overflow.
Run Status: Wrong Answer
Progress: 13/17 test cases passed.
input output expected
-2147483648, 1 0 -2147483648 |
|
c*******9 发帖数: 6411 | 9 in a 32 bit 2-compliment system:
evaluate the following expression:
-2147483648 < -21474836487
(unsigned) -2147483648 < -21474836487
(unsigned) -2147483648 < 21474836487 |
|
g*********s 发帖数: 1782 | 10 int atoi(const char* s);
assuming INT_MAX = 2^31-1, INT_MIN = -2^31, the following must hold:
atoi("2147483648") = 2147483647
atoi("-2147483649") = -2147483648
but this seems a little tricky. any elegant solution to handle the
overflow? |
|
w****x 发帖数: 2483 | 11 int myatoi(const char* p)
{
assert(p);
bool bNeg = false;
if ('-' == *p || '+' == *p)
{
bNeg = '-' == *p;
p++;
}
if (*p == 0) throw CException();
//use unsigned to deal with INT_MIN
unsigned int nAbsValue = 0;
while (*p != 0)
{
if (*p < '0' || *p > '9')
throw CException();
unsigned int nOrgValue = nAbsValue;
int nDigit = *p - '0';
nAbsValue = 10*nAbsValue + nDigit;
//deal with normal o... 阅读全帖 |
|
i**********e 发帖数: 1145 | 12 "2147483647","-2147483648","2147483648","-2147483649"。
这几个 test case 测试你处理溢出的边界条件,很重要。
还有就是前面有空格的 case 吧,例如 " 23" 等。 |
|
m*****n 发帖数: 204 | 13 try -2147483648 / -2147483648
My code got into infinite loop. Looks like your code have the same problem. |
|
m********c 发帖数: 105 | 14 abs(INT_MIN)结果还是-2147483648,不是2147483648,所以要转换成long long,这是
一个原因,好像还有其他原因 |
|
k******4 发帖数: 94 | 15 unsigned int newx = x;
If (x<0)
newx = 1+abs(1+x);
abs(INT_MIN)结果还是-2147483648,不是2147483648,所以要转换成long long,这是
一个原因,好像还有其他原因 |
|
a*****a 发帖数: 46 | 16 select if(s=2147483648, null, s) from (
(select 2147483648 as s)
union all
(select distinct Salary as s from Employee)
) x
order by s Desc limit N, 1
这个可以过,但估计不是很好 |
|
T*****u 发帖数: 7103 | 17 Leetcode divide two integers
why ???
Submission Result: Wrong Answer
Input:
-2147483648, -1
Output:
2147483648
Expected:
2147483647
|
|
z********i 发帖数: 60 | 18 Using io.Outputstream, I need to write some 32 bit integer to the
stream. any value is fine except -2147483648 and 2147483648. I tried
to just mask 32 bits and cut them in bytes. The bitstream is just
ended with them without any warning.
Thank you for taking time. |
|
g*********s 发帖数: 1782 | 19 【 以下文字转载自 JobHunting 讨论区 】
发信人: gandjmitbbs (Nothing), 信区: JobHunting
标 题: 经典题atoi的溢出处理
发信站: BBS 未名空间站 (Tue Feb 22 21:40:03 2011, 美东)
int atoi(const char* s);
assuming INT_MAX = 2^31-1, INT_MIN = -2^31, the following must hold:
atoi("2147483648") = 2147483647
atoi("-2147483649") = -2147483648
but this seems a little tricky. any elegant solution to handle the
overflow? |
|
s****n 发帖数: 700 | 20 那看看这个code
#include
using namespace std;
int main() {
int i;
unsigned int ui;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
i = 1 << (sizeof(i) * 8 - 1);
cout << "sizeof(i) is " << sizeof(i) << endl;
cout << "i is " << i <
ui = 1 << (sizeof(ui) * 8 - 1);
cout << "sizeof(ui) is " << sizeof(ui) << endl;
cout << "ui is " << ui <
l = 1 << (sizeof(l) * 8 - 1);
cout << "sizeof(l) is " << sizeof(l) << endl;
cout << "l is " << l <
ul = 1 << (sizeof(ul) * 8 -... 阅读全帖 |
|
|
n**e 发帖数: 116 | 22 看到版上太多关于atoi的讨论,我贴一个我的实现,希望对有所帮助。如发现bugs,请
指正。
//--------------------------------------------------------------------
// Convert a string to an integer
// How to handle overflow issue:
// Compute the cutoff value between legal numbers and illegal
// numbers. That is the largest legal value, divided by the
// base. An input number that is greater than this value, if
// followed by a legal input character, is too big. One that
// is equal to this value may be valid or not; the limit
// between... 阅读全帖 |
|
m***o 发帖数: 41 | 23 A non-empty zero-indexed array A consisting of N integers is given. A pair
of integers (P, Q) is called K-complementary in array A if 0 = P, Q < N and
A[P] + A[Q] = K.
For example, consider array A such that
A[0] = 1 A[1] = 8 A[2]= -3
A[3] = 0 A[4] = 1 A[5]= 3
A[6] = -2 A[7] = 4 A[8]= 5
1,8,-3,0,1,3,-2,4,5
The following pairs are 6-complementary in array A: (0,8), (1,6), (4,8), (5,
5), (6,1), (8,0), (8,4). For instance, the pair (4,8) is 6-complementary
because A[4] + A[8] = 1 + 5 = 6.... 阅读全帖 |
|
n**e 发帖数: 116 | 24 How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the... 阅读全帖 |
|
n**e 发帖数: 116 | 25 How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input bas... 阅读全帖 |
|
n**e 发帖数: 116 | 26 How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the... 阅读全帖 |
|
n**e 发帖数: 116 | 27 How to handle overflow issue on atoi? Hopefully this will be of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the... 阅读全帖 |
|
n**e 发帖数: 116 | 28 How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input bas... 阅读全帖 |
|
n**e 发帖数: 116 | 29 How to handle overflow issue on atoi? Hope this is of help.
Compute the cutoff value between legal numbers and illegal numbers. That is
the largest legal value, divided by the base. An input number that is
greater than this value, if followed by a legal input character, is too big.
One that is equal to this value may be valid or not; the limit between
valid and invalid numbers is then based on the last digit.
For instance, if the range for longs is [-2147483648..2147483647] and the
input bas... 阅读全帖 |
|
i**********e 发帖数: 1145 | 30 因为要测试边界条件啊,因为你知道 2147483647 没有溢出,而 2147483648 溢出了,
所以测试 positive 溢出就这两个 case 就够了。
negative 溢出也就是其余两个 case. |
|
|
n**e 发帖数: 116 | 32 I posted my implementation before. Here is my handling with overflow issue.
//----------------------------------------------------------------
// Compute the cutoff value between legal numbers and illegal
// numbers. That is the largest legal value, divided by the
// base. An input number that is greater than this value, if
// followed by a legal input character, is too big. One that
// is equal to this value may be valid or not; the limit
// between valid and invalid numbers is... 阅读全帖 |
|
C***U 发帖数: 2406 | 33 我把dividend和divisor存到x y里面都是unsigned int了
下面这个代码对了 通过leetcode的验证了 只不过写的很丑
把最小的数拿出来单独处理了
int divide(int dividend, int divisor) {
int flag = 1;
unsigned long int x;
unsigned long int y;
unsigned long int z;
unsigned int i = 0;
unsigned int count = 0;
int result = 0;
if(!divisor) {
cout << "could not be divided by 0" << endl;
return 0;
}
if(dividend == -2147483648) {
if(divisor > 0) {
dividend += divisor;
result ... 阅读全帖 |
|
B*******1 发帖数: 2454 | 34 Try
INT_MIN -2147483648 |
|
c********t 发帖数: 5706 | 35 试了试,还真不行,因为过不了 dividend=-2147483648,绝对值变0, 结果为0。
这个点很tricky. |
|
c********t 发帖数: 5706 | 36 目测 似乎有个错 {-2147483648,2147483647} 啥结果? |
|
c********t 发帖数: 5706 | 37 感觉你和ls有一样的错 测测{-2147483648,2147483647}
如果你过了大case, 也许是leetcode没这样的case, 也许是我错了 |
|
g***j 发帖数: 1275 | 38 Divide two integers without using multiplication, division and mod operator.
这个题目怎么做呀,我用的是数学公式exp(logx - logy)
但是,large set就这一个通不过,难道这个除出来不是-1么?他说expected是0,这是
为什么?
2147483647, -2147483648 -1 0
|
|
r*c 发帖数: 167 | 39 贴个pattern字符串有重复字符的解法, 是dek,cpp1等大牛的解法的扩展。
#include
#include
#include
#include
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN -2147483648
class MinWindowSolution
{
public:
struct TreeNode
{
TreeNode *parent;
int val;
vector children;
TreeNode(int i, TreeNode *p) : val(i), parent(p){}
};
void FindMinWindow_Tree(const vector& input , const vector&
query , int& nStart,... 阅读全帖 |
|
|
w********g 发帖数: 106 | 41 which negative input causes problem? I checked 0, -8, -16, and -2147483648
(-2^31), all return true. |
|
w********g 发帖数: 106 | 42 which negative input causes problem? I checked 0, -8, -16, and -2147483648
(-2^31), all return true. |
|
|
m**********g 发帖数: 153 | 44 -2147483648 是MIN_INT吧? overflow |
|
w*******8 发帖数: 10 | 45 来自主题: JobHunting版 - M 家电面 是小弱不是大牛
正刷题呢,看到是leetcode上原题就顺手把代码搬过来了
Divide Two Integers:
思路是:既然是int的除法,不用考虑小数部分(余数),就是整数(商),那就数
dividend 里有多少个divisor. 这样,如果商是n 需要O(n)计算,可以用下二分法,
divide&conquer 递归调用一下,就是lgN了
剩下的就是边界条件: 1)正负 2)极值
int 的范围是 -2,147,483,648 to 2,147,483,647
这里有个细节,当时看别人的代码觉得比较巧妙:
正负数的范围是不对称的
-2147483648 abs 一下还是自己本身,那就先count一次,再计算
之前看到很多方法是转成double 再强转回int,其实是没必要的
其他基本的int 计算也大多是这个思路 比如 int sqrt(int) int pow(int) |
|
U***A 发帖数: 849 | 46 来自主题: JobHunting版 - M 家电面 -2147483648 abs 一下还是自己本身,?
这个对吗? |
|
m**r 发帖数: 574 | 47 public int isPowerOfTwo(int x)
{
return(
x == 1 || x == 2 || x == 4 || x == 8 || x == 16 || x == 32 ||
x == 64 || x == 128 || x == 256 || x == 512 || x == 1024 ||
x == 2048 || x == 4096 || x == 8192 || x == 16384 ||
x == 32768 || x == 65536 || x == 131072 || x == 262144 ||
x == 524288 || x == 1048576 || x == 2097152 ||
x == 4194304 || x == 8388608 || x == 16777216 ||
x == 33554432 || x == 67108864 || x == 134217728 ||
x == 268435456 || x == 53... 阅读全帖 |
|
h******o 发帖数: 6 | 48 bool helper(TreeNode *node, int min, int max) {
if (node == nullptr) return true;
return node->val < max && node->val > min && helper(node->left, min
, node->val) && helper(node->right, node->val, max);
}
bool isValidBST(TreeNode *root) {
if (root && (root->val == INT_MIN || root->val == INT_MAX)) {
if (!root->left && !root->right) {
return true;
}
}
return helper(root, INT_MIN, INT_MAX);
}
fail {-21... 阅读全帖 |
|
z*****6 发帖数: 29 | 49 Return value定义的是int吧,int范围是-2147483648 ~ 2147483647的。
你的答案其实算是overflow了吧。 |
|
j***a 发帖数: 10844 | 50 http://criswellhonda.net/New-Inventory.aspx#ajax_t=129771261611 Auto LX&pagesize=25&virtualpageindex=0&modelyear=2011&numberofvisiblepages=10&sortitem=-2147483648&hyperlinkargument=page&model=Civic Sdn&sortdirection=True
差5000块,不大可能吧?
From Blue Book:
$19,305.00 MSRP
$18,243.00 Fair Purchase Price
$17,843.65 Invoice
$3800 below invoie? Never heard of such a deal.
实的,在criswell买的。还送了两年oil change和洗车。百分之0.9的利率。
不降价,你爱买不买的样子。 |
|