polycom onsite

// Please do the following.
// 1. void push(Object o);-- add element to the top
// 2. Object pop(); -- remove element from the top
// 3. boolean isEmpty(); -- true/false if stack is empty

// Queue q;
// void enqueue(Object o); -- add to the end of the queue
// Object dequeue(); -- remove from the front of the queue
// boolean isEmpty(); -- true/false if the queue is empty


queue 3 2 1 3 queue 2 1 size-1
stact pop 3


class stack{


queue firstq = new queue();
queue secq = new queue();

public void push(Object o)
{
firstq.enqueue(o);
}

public Object dequeue(){
if(firstq.isEmpty())
throw /*new */RuntimeException("size == 0");
object result = null;

while(1)
{
Object o = firstq.pop();
if(firstq.isEmpty())
{ result = o;
break;
}
else{
secq.enqueue(o);
}
}

// optimize below

queue buf = firstq;
firstq = secq;
secq = firstq;
// till here

return result;
}

public boolean isEmpty(){

return firstq.isEmpty();
}

}


// 2. "Hi, good morning"
// -> "morning good Hi,"

gninorom doog ,ih

public void revserAllwords(char[] str, int i , int j)
{

while(i<j)
{
char buf = str[j];
str[j] = str[i];
str[i] = buf;
i++;
j--;
}
return;
}

reverswor

public void revserfuction(char[] str)
{

int wordStart = 0;
int wordEnd = 0;
revserAllwords(str,0,str.length-1);
for(int i =0; i < str.length ; i++){
if(char[i]==' '){
wordEnd = i;
revserAllwords(str,wordStart,wordEnd);
wordStart = wordEnd+1;
}else{
continue;
}
}

//duel with last word
revserAllowords(str,wordStart,str.length-1)

return;
}

// option 3
// static memory
int bufferForDecompression[256*1024]; // ??

static memory

// can't take longer than 15 ms
void realTimeFoo( struct message ) {
int x = 1024;
// Option 1


// Option 2
int profileTimeStart = GetTime();

// the heap is fragmented....
int threshold = max ;// max < 10ms buf[8000] 10ms

if(x> 8000/256){
return new RuntimeExcepiton("reset x");
}



int[] bufferForDecompression = new bufferForDecompression( 256 * x ); // on the heap but how long does this take ???
int profileTimeStop = GetTime();

int delta = profileTimeStop - profileTimeStart; // measured 100x and it took 10 ms which is less than 16 ms

bool isOk = false;
isOk = decompress( message, bufferForDecompression );

return isOk;

stack store some:
return addreess
}

stack:

 

 

 

Given an positive integer array of size N, and you are given the standard rand function. Ex. you can choose each number with a probability 1/N

Can you write a custom rand function such that you can choose each number in the array so that it is proportional to the value of the integer.

Ex: {1, 2, 3} . If probability of choosing 1 is x, probability of choosing 2 should be 2x.


int rand(arr[]){
// return an integer in the array with probabilityu of 1/N (arr size)
/ can be 1 or 2 or 3

}

arr = {1,2,3}
rand 1/3 2/3 3/3
int myrand(arr[]){
//return with probability proportional to integer
// you should return either 1 or 2 or 3 .... but with probability proportional to the number (not 1/N)

1 1/N 2 : 2/N 3:3/N

1,2,3 1,2,2,3,3,3
bufArry = new

}

If N is very large, it's not a good method (because you use lot of memory)

1 , 2 , 5

1/8 2/8 5/8

1*rand() + 2*rand() + **** + ......5* rand() / N.   

define your own sizeof:

#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)

转载于:https://www.cnblogs.com/leetcode/p/3258804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值