由买买提看人间百态

topics

全部话题 - 话题: integ
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
l********y
发帖数: 1327
1
设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
比如:9=4+5 或者9=2+3+4, 但是8就不可以
奇数都可以,但是偶数怎么判断?
c*******t
发帖数: 1095
2
来自主题: JobHunting版 - 问到题:reverse an integer
function:
简单点
比如输入123,输出321
输入100,输出1
如何简单判断reverse以后的数溢出了没有?比如integer是1byte的,那范围就是-128~
+127,如何判断reverse 126 是溢出的?
j**y
发帖数: 462
3
来自主题: JobHunting版 - array contains two integer that sum up to 7
detect if a sorted array contains two integer that sum up to 7. And then
improve your code so that the array is accessed with only one iteration
any solution for one iteration?
s**x
发帖数: 405
4
来自主题: JobHunting版 - switch odds and evens bits of an integer
this is incorrect because right-shift of signed integer duplicates the most
significant bit.
B*******1
发帖数: 2454
5
来自主题: JobHunting版 - 任意输入STRING,转换为INTEGER,
atoi
function

int atoi ( const char * str );
Convert string to integer
Parses the C string str interpreting its content as an integral number,
which is returned as an int value.
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.
Y******l
发帖数: 19
6

, and then run the first algorithm.
just any integer number?
是啊,我也有同样的疑问,如果N不是2^m.. 有什么快速的方法吗?
k***t
发帖数: 276
7
第一个异或。第二个或(初值零)或者与(初值为up to N bit 全一,与每个element的反)。

, and then run the first algorithm.
just any integer number?
c*****e
发帖数: 737
8
【 以下文字转载自 Programming 讨论区 】
发信人: coollpe (coollpe), 信区: Programming
标 题: 面试被问了议题: check if an integer is power of 2
发信站: BBS 未名空间站 (Wed Feb 15 10:16:41 2012, 美东)
我首先回答了每次左移移1位比较,然后面试官不满意,我又想出来bitmap,开个64k数
组,他说能不能只有1条语句的判断,我死活想不出来。
后来回家google发现了 x & (x-1)这个trick。不过我觉得如果没看到过的话要在面试
时间内想出来简直不可能。
测试了一下,bitmap的速度要比 x && (x & (x-1))快一些。但机器不同结果也不同。
回家我又想了若干方法
1, popcnt (x) == 1, 新处理器已经可以在1个指令周期内完成。
2, ffs(x)查表,这个表大概只要存32个整数,占用存储小很多,速度和x & (x-1)差不多
3, asm bsrl, asfl, 2个指令周期+1次比较,和x && (x & (x-1)一样快
h*****f
发帖数: 248
9
来自主题: JobHunting版 - find the first missing positive integer.
不知道我是不是解错题,如果输入的是[11,9,7,5],那第一个missing positive
integer= 4?
or the absolute value of the number must be between 0 and last index+1?
h*****f
发帖数: 248
10
来自主题: JobHunting版 - find the first missing positive integer.
不知道我是不是解错题,如果输入的是[11,9,7,5],那第一个missing positive
integer= 4?
or the absolute value of the number must be between 0 and last index+1?
b*******e
发帖数: 217
11
来自主题: JobHunting版 - find the first missing positive integer.
build a min-heap in o(n).
query the min element.
query the two child ot he min element.
if the three elements not consecutive. find the missing interger already.
Otherwiese, go to the smaller child until the missing integer is found. o(
logn)
complexity = o(n) + o(logn)
b*******e
发帖数: 217
12
来自主题: JobHunting版 - find the first missing positive integer.
build a min-heap in o(n).
query the min element.
query the two child ot he min element.
if the three elements not consecutive. find the missing interger already.
Otherwiese, go to the smaller child until the missing integer is found. o(
logn)
complexity = o(n) + o(logn)
d****o
发帖数: 1055
13
来自主题: JobHunting版 - Divide Two Integers
这道题除了-n次以外,还有更好的办法吗?
Divide two integers without using multiplication, division and mod operator.
l*********8
发帖数: 4642
14
来自主题: JobHunting版 - Divide Two Integers
How to divide two Long integers then?
d****o
发帖数: 1055
15
来自主题: JobHunting版 - Divide Two Integers
这道题除了-n次以外,还有更好的办法吗?
Divide two integers without using multiplication, division and mod operator.
l*********8
发帖数: 4642
16
来自主题: JobHunting版 - Divide Two Integers
How to divide two Long integers then?
l**b
发帖数: 457
17
来自主题: JobHunting版 - Divide Two Integers
ColdKnight,你去看看Math.abs(Integer.MIN_VALUE)输出的是什么你就知道为什么TLE了
h****e
发帖数: 928
g***j
发帖数: 1275
19
来自主题: JobHunting版 - Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
这个题目怎么做呀,我用的是数学公式exp(logx - logy)
但是,large set就这一个通不过,难道这个除出来不是-1么?他说expected是0,这是
为什么?
2147483647, -2147483648 -1 0
h**o
发帖数: 548
20
来自主题: JobHunting版 - leetcode: integer to roman 结果不同
那个unordered_map 是从roman to integer 拷的, 因为懒得再敲一遍,
目的就是得到 roman 和 int 的配对。和你建议的是一个意思。 我把 v 改写成这样好
了:
vector> v;
v.push_back(make_pair("M",1000));
v.push_back(make_pair("CM",900));
v.push_back(make_pair("D",500));
v.push_back(make_pair("CD",400));
v.push_back(make_pair("C",100));
v.push_back(make_pair("XC",90));
v.push_back(make_pair("L",50));
v.push_back(make_pair("XL",40));
v.push_back(make_p... 阅读全帖
h**o
发帖数: 548
21
来自主题: JobHunting版 - leetcode: Divide Two Integers 怎么做?
题目: Divide two integers without using multiplication, division and mod
operator.
应该是怎么做哪?leetcode 有个人气解答如下,但是我看不懂, 有谁给解释一下你怎
么做的?
int divide(int dividend, int divisor) {
long long a = abs((double)dividend);;
long long b = abs((double)divisor);
long long ret = 0;
while (a >= b) {
long long c = b;
for (int i = 0; a >= c; ++i, c <<= 1) {
a -= c;
ret += 1 << i;
}
}
return ((dividen... 阅读全帖
l*n
发帖数: 529
22
每进一位反向检测一下就好了,很直观,虽然效率上可能不是最好的。
tmp=sum*10+x;
if ((tmp-x)/10!=sum) {
//overflow
}
https://www.securecoding.cert.org/confluence/display/java/NUM00-J.+Detect+or
+prevent+integer+overflow
这有看着比较详细的,但是感觉代码太繁冗了。
i******s
发帖数: 301
23
你是说找最小positive integer那种?你提的算法都不对啊。。。扫两遍,第一遍交换
A[i]和A[A[i]-1](假设输入数组是int[] A),直到没法交换就i++。第二遍从0扫到A.
length, 遇上A[i] != i+1就返回。什么hash, bit vector,看不懂。。。
s**o
发帖数: 30
24
看面经的时候看到大家说过big integer plus one,请问这题愿意是什么,我不知道完
整的问题是什么,google了一下说牵扯到string,这是怎么回事?谢谢指教!
a***e
发帖数: 413
25
来自主题: JobHunting版 - Divide Two Integers OJ和CCP150的做法
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
这个Ccp150上那种做法(也就是我一开始做的直接减的方法会TLE)
是不是说明现在面试要求比以前高很多?
还在试图在原来的code上面改变以加快
class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor==0) return INT_MAX;
int r=0;
if (dividend==0) return r;
int flag=sign(dividend)*sign(divisor);
dividend=abs(dividend);
divisor=abs(divisor);
dividend=dividend-divisor;
w... 阅读全帖
y*****e
发帖数: 712
26
L家最爱考的面试题之一就是nested integer了,
还爱考各种iterator的implementation
这题是把两个最爱合在一起了。。。。感觉很有可能出,但网上没找到满意的答案.
题目是这样的
eg: {{1,2},3,{4,{5,6}}}
不断调用iterator的next()返回的序列是 1 2 3 4 5 6
这个data structure的interface是这样的
public interface Data {
// Does this Data hold a collection?
public boolean isCollection();
// Returns the collection contained by this Data, or null if it is a
single element
public Collection> getCollection();
// Returns the single element contained by this Data, or nul... 阅读全帖
T*****u
发帖数: 7103
27
来自主题: JobHunting版 - Help...leetcode divide two integers
Leetcode divide two integers
why ???
Submission Result: Wrong Answer

Input:
-2147483648, -1

Output:
2147483648

Expected:
2147483647

g*c
发帖数: 4510
28
https://leetcode.com/problems/integer-to-english-words/
出这种题的面试官就是希望你出错然后废了你吧
w*s
发帖数: 7227
29
据说用数字做索引,在数据库里比较快。
请问怎么把stock ticker name 转换成 integer ID。
注意有些股票会delist, 会改ticker,又会有新上市的。
怎样一次性地改成ID ?
谢谢!
s*******1
发帖数: 40
30
Hi,
If I want to solve a integer programming with several thousand of
constraints,
which software will be good for solving this problem?
Thanks!
m*****F
发帖数: 458
31
I think it's 1 bit.
For example, 8-bit integer.
0100110
number of ON bits are 3.
t******e
发帖数: 1293
32
我的规模可能是1000个约束,变量也大约1000个左右
matlab里面的integer programming solver能handle吗?
谢谢
P********s
发帖数: 15
33
Here is the code snippet ( managed C++)
...
Int32 nTemp = 10;
String * pMsg = String::Format("Display an integer here: {0}", nTemp);
...
I got an error message when tried to compile the program:
error C2665: 'System::String::Format' : none of the 5 overloads can convert pa
rameter 2 from type 'int'
Am I missing anything here? Thanx in advance.
P********s
发帖数: 15
34
Thanks for the reply. I also found another solution:
String::Format("....", __box(nTemp));
The __box() keyword wraps the integer as an object.

nTemp.ToString
()
then ok
m****r
发帖数: 11
35
来自主题: Java版 - Integer in heap
If the heap keeps increasing, it is a memory leak. You need to check who
references
those Integers. java profilers shall provide reference graph. which profiler
are you using?

up
m******t
发帖数: 2416
36
来自主题: Java版 - Integer in heap

up
Note that unless you have -incgc turned on, the GC may not kick in until
there isn't any free memory left, i.e., you might not see these Integers
GCed even though they are not in use any more.
d*******n
发帖数: 524
37
那么int[]跟Integer[]可以mutually交换使用么?
如果有个method是这么定义的
public class SomeClass {
public void f(T[] array);
}
现在想对int[]使用SomeClass的f方法,怎么办?
(就像在C++里面那样,任何用template的地方既可以用class,也可以用int)
b******y
发帖数: 9224
38

for
I think you'd have to do a loop and get the value from the Integer array and
store the int value into the int array.
Example:
int[] intArr = new int[integerArr.length];
for (int i = 0; i < integerArr.length; i++)
{
intArr[i] = integerArr[i];
}
s********k
发帖数: 6180
39
【 以下文字转载自 JobHunting 讨论区 】
发信人: silverhawk (silverhawk), 信区: JobHunting
标 题: linux怎么读入一个超过有超过1B integer的binary file?
发信站: BBS 未名空间站 (Fri Oct 26 10:40:33 2012, 美东)
用什么最好?fread,fseek或者先用mmap?如果内存一下装不下,怎么能分段读?
谢谢
w*s
发帖数: 7227
40
【 以下文字转载自 Programming 讨论区 】
发信人: wds (大盘5000不是梦), 信区: Programming
标 题: Perl Q: how to convert integer to MAC address
发信站: BBS 未名空间站 (Fri Mar 24 23:10:08 2017, 美东)
81952921372024 => 78:45:c4:26:89:4a
求简洁明快的perl程序
谢谢!
q**a
发帖数: 75
41
来自主题: Programming版 - how to convert an integer to a widestring
In windows, using c/c++, how to convert an integer to a wide string?
Thanks.
b***y
发帖数: 2799
42
☆─────────────────────────────────────☆
jyu (jyu) 于 (Fri Mar 14 16:55:32 2008) 提到:
hi All,
I have a problem casting a 4 byte unsigned char into integer.
here is my code:
this is C++ code:
// header is just a structure with a field of 4 byte unsigned char
unsigned int sequence = *(reinterpret_cast(header->m_sequenceNum
));
and the sequence is always some overflow #!
all comments/help are appreciated!
Thank you very much!
☆─────────────────────────────────────☆
Xentar (思考猪) 于
p********n
发帖数: 98
43
有一个64位长的bitarray(0 1 0 1...),如何最快转换成integer?
笨方法是加一个64的循环,往目标整数里填(一位一位的移),或者拆成两个32位填(
快一点点),还是太慢,还有什么其它简单快捷方法没有?谢谢
c*****e
发帖数: 737
44
测试了一下几种算法
gcc 4.1.2, -O2
Xeon E5335 2 GHz
64k个random integer
Winner: cost 328188
inline bool isPower(int x_)
{
register int bitpos, bitpos2;
asm ("bsr %1,%0": "+r" (bitpos):"rm" (x_));
asm ("bsf %1,%0": "+r" (bitpos2):"rm" (x_));
return (bitpos == bitpos2) && x_;
}
Second: cost 361644
就是查64k表法
bool isPower(int x_)
{
return b[x_];
}
Third: cost 405918
bool isPower(int x_)
{
return x_ && (x_ & (x_ - 1)) == 0;
}
Forth: cost 434838
int a2[] = {-1,1,2,4,8,16,32,64,128,256,512,1024,2048,40... 阅读全帖
c*****e
发帖数: 737
45
测试了一下几种算法
gcc 4.1.2, -O2
Xeon E5335 2 GHz
64k个random integer
Winner: cost 328188
inline bool isPower(int x_)
{
register int bitpos, bitpos2;
asm ("bsr %1,%0": "+r" (bitpos):"rm" (x_));
asm ("bsf %1,%0": "+r" (bitpos2):"rm" (x_));
return (bitpos == bitpos2) && x_;
}
Second: cost 361644
就是查64k表法
bool isPower(int x_)
{
return b[x_];
}
Third: cost 405918
bool isPower(int x_)
{
return x_ && (x_ & (x_ - 1)) == 0;
}
Forth: cost 434838
int a2[] = {-1,1,2,4,8,16,32,64,128,256,512,1024,2048,40... 阅读全帖
s********k
发帖数: 6180
46
【 以下文字转载自 JobHunting 讨论区 】
发信人: silverhawk (silverhawk), 信区: JobHunting
标 题: linux怎么读入一个超过有超过1B integer的binary file?
发信站: BBS 未名空间站 (Fri Oct 26 10:40:33 2012, 美东)
用什么最好?fread,fseek或者先用mmap?如果内存一下装不下,怎么能分段读?
谢谢
s********k
发帖数: 6180
47
来自主题: Programming版 - 一个integer promotion问题
我也觉得你的这个是对的。跑了一下也是
https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+
Understand+integer+conversion+rules
我是看这个链接,里面说
because uc is equal to UCHAR_MAX, which is equal to UINT_MAX, the addition
results in an overflow in this example.
十分不解,看来这个是错的。
b*******t
发帖数: 34
48
来自主题: Programming版 - INTEGER搜索求建议
我就是要性能,如果集合可能是包括大多数可表示的正整数,踫撞会很多,这样键哈希
表时每个哈希筒里会不断需要插入,这样以后査询才会快,感觉建表的开销太大了吧。
一般哈希表会有多少bucket?如果我的集合有2^32-1,每个bucket会有很多元素
简单的方法是先排序,査询时二叉搜索,但这样cache效率会比较差,因为搜索时会到
处跳

integer
q***e
发帖数: 90
49
How can I use a simple usnix/linux command to
convert a date string(e.g. 10:00pm Wed. Oct. 2nd 1999 -500)
into an integer(the number of seconds as from the UNix epoc).
Don't writing a C function. Hope to use a simple command,or
some awk, sed script. Just used to parse a date in log files.
Thanks a lot!
k**e
发帖数: 86
50
移位运算符 + print语句
for example,
print ( "%d", (integer >> n) & 0x1 )
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)