Redian新闻
>
推荐人还是加上了他发表的文章的数目
avatar
推荐人还是加上了他发表的文章的数目# Immigration - 落地生根
f*e
1
Determine whether an integer is a palindrome. Do this without extra space.
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
return false;
int y = x;
return helper(x, y);
}

public boolean helper(int x, int y){
if(x == 0)
return true;
if (helper(x/10, y) && (x % 10 == y % 10) ){

y =y/ 10;
return true;
}
else
return false;
}
}
问题在于, 怎样用java 取代, c++ pointer. y return后, 一直都没变. 请大家帮帮忙
.
avatar
w*0
2
130多篇,哎。。
怎么办,我还用这信吗?
avatar
s*n
3
用个自定义的数据结构
class MyInteger{
public int data;
public MyInteger(int d){ data=d;}
}
public boolean isPalindrome(int x) {
if(x<0) return false;
if(x==0) return true;

return isPalin(x, new MyInteger(x));
}
public isPalin(int x, MyInteger y){
if(x==0) return true;
if(isPalin(x/10, y) && (x%10==y.data%10)){
y.data=y.data/10;
} else {
return false;
}
return true;
}
avatar
a*n
4
sure it can be used
avatar
f*e
5
非常感谢.
avatar
w*0
6
不是说移民官会用来比较吗?不过这个是欧洲的推荐人,好不容易找到的,也不想放弃。

【在 a***n 的大作中提到】
: sure it can be used
avatar
w*3
7


【在 f**********e 的大作中提到】
: 非常感谢.
avatar
i*t
8
明确告诉他 去掉 io会拿来比 对你不利
avatar
w*3
9
int y定义在method外面做solution的member variable,应该work不过不知道是不是
good practice。

【在 f**********e 的大作中提到】
: 非常感谢.
avatar
s*u
10
try to tell him
if not ok, just use it.
io will base on your overall case, not this single number.
avatar
f*e
11
试过, 不行.

【在 w****3 的大作中提到】
: int y定义在method外面做solution的member variable,应该work不过不知道是不是
: good practice。

avatar
w*0
12
好吧,我试试看吧,希望他能理解
实在不行也就用吧,似乎版上也有人有同样的情况,毕竟io还是要看整个的package...

【在 s*********u 的大作中提到】
: try to tell him
: if not ok, just use it.
: io will base on your overall case, not this single number.

avatar
y*9
13
新建integer或者new一个东西不算用了extra space?
题想说用constant space?
avatar
C*y
14
最好不用
有隐患的一律不要用, 别有侥幸心理, 否则被RFE了就不要怪IO了
avatar
y*9
15
可不可以用这个pseudo code:
if (x < 0){
return false;
}
if (x == 0){
return true;
}
int k = 0;
int b = int(log x / log 10));
while(k <= b/2){
if (x/ (10^k) % 10 != x / (10^(b-k)) %10){
return false;
}
k++;
}
return true;
avatar
S*M
16
别紧张。我的所有推荐人都有文章数目100+在上面,还列了大奖。相比之下,每个推荐
人都比我强一大截。没事。
avatar
s*x
17
I believe leetcode provide a simple solution which may overflow, but
actually it is correct.
You can simply use unsigned int. I would think even an int would work as
well.
bool isPalindrome(int num) {
if(num < 0) return false;
unsigned int oldNum = num;
unsigned int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev == oldNum);
}
The following is from gnu comments.
13.2.1 Basics of Integer Overflow
In languages like C, unsigned integer overflow reliably wraps around; e.g.,
UINT_MAX + 1 yields zero. This is guaranteed by the C standard and is
portable in practice, unless you specify aggressive, nonstandard
optimization options suitable only for special applications.
In contrast, the C standard says that signed integer overflow leads to
undefined behavior where a program can do anything, including dumping core
or overrunning a buffer. The misbehavior can even precede the overflow. Such
an overflow can occur during addition, subtraction, multiplication,
division, and left shift.
Despite this requirement of the standard, many C programs and Autoconf tests
assume that signed integer overflow silently wraps around modulo a power of
two, using two's complement arithmetic, so long as you cast the resulting
value to a signed integer type or store it into a signed integer variable.
If you use conservative optimization flags, such programs are generally
portable to the vast majority of modern platforms, with a few exceptions
discussed later.
For historical reasons the C standard also allows implementations with ones'
complement or signed magnitude arithmetic, but it is safe to assume two's
complement nowadays.
Also, overflow can occur when converting an out-of-range value to a signed
integer type. Here a standard implementation must define what happens, but
this might include raising an exception. In practice all known
implementations support silent wraparound in this case, so you need not
worry about other possibilities.
avatar
c*n
18
如果他是老professor,名头很大,just use it
如果推荐人很年轻,或者是个AP,建议麻烦他修改一下
avatar
w*3
19
可以的啊。我试了一下是work的,如果y定义在variable里面就不用当参数传进去了。

【在 f**********e 的大作中提到】
: 试过, 不行.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。