在线等a[i][0]=1;characters.&quot翻译;,len);length(p)

一道C++的题,非常急!!!在线等!!!!!答后追加高分!!!!!_百度知道
一道C++的题,非常急!!!在线等!!!!!答后追加高分!!!!!
Example:Last_ and the output file name must be &quot, and Last in the fooutfile&quot.
And each person must have F:Suppose the input file consists of (special consideration is required for the case &quot, Edward D.Cain.
There are unknown number of persons in input files, however. HornJohn Jerry DoeEdward David CainThen the output file should contain, each person's name occupies a line:Horn, Mary J;Middle/Middle IMary J. Horn&quot, John J;).The input file name must be &quot.Dinfile&quot:First_name Middle_name&#47.
The first character of First&#47, First_name Middle_initial:Mary J;Middle_initial Last_nameThe program then outputs (to a file) the name in the following format (a period must be place right after the Middle_initialWrite a program that reads names (from an input file) in the following format (you can assume names are no more than 30 characters if you are using C-string);Last name must be capitalized), Middle&#47
提问者采纳
if(。#include &: -1, &lineSize) == 0)
string strLine =
outLine +=
len = offset + (int)(strlen(*buf + offset));
fclose(infile); &quot, strLine既然是C++,我图省事写成全局的了, firstSpacePos); &quot, &quot: (ferror(fp)) .size().substr(firstSpacePos + 1, &
string lastName = strL
strcpy(&lt, &
outLine += &
string firstName = strL
string middle = strL}int main(){
FILE * infile = fopen(&quot? -2 ;&#92, *bufsize - &
outLine += &n'
return 0;);
int secondSpacePos = strL&iostream&)
(*buf)[len - 1] = 0;
outLine += &;
FILE * outFile = fopen(&
fwrite(outL
while (readLine(outfile&
return 0, int *bufsize){
char * newbuf = 0;
delete[] *
return -1, sizeof(char); &
char *line = 0;;&#92!&
fclose(outFile).size() - secondSpacePos - 1).substr(secondSpacePos + 1;
if (!newbuf)
return -2;&
int firstSpacePos = strL
int len = 0;
newbuf = 0;
return -1;);; & &
*bufsize *= 2;
offset = len.substr(0;
delete[]infile&int readLine(FILE * &quot, outLine, *buf);
int lineSize = 0;!= 0) !&quot!fgets(*buf + offset.find(&&
if(, char **buf, firstSpacePos + 1);
int offset = 0, fp))
return (;w&n&
return 0, secondSpacePos - firstSpacePos - 1)? 0 ;Create the outF
if (infile == 0)
*bufsize = 128;
if(*buf == 0)
*buf = new char[128]!*buf)
return -2.find(&
outLine += firstN
if (outFile == 0)
if((*buf)[len - 1] == &#39, 0);
new char[*bufsize * 2];
string outLine = lastN
*buf = newbuf.c_str(), outFile),最好把readLine这个封装到类里面
提问者评价
太感谢了!!!但你写的好像太高深了,有的我还没学呢,呵呵,多谢了!!!加个好友吧!!!
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁请帮我翻译一下这段文章(英文)在线等_百度知道
请帮我翻译一下这段文章(英文)在线等
海上霸王都是他们向往的头衔,越纯,在无辜者即将落难时,虽然不能给我们做模范.人总是会长大的:为了去冒险,有趣的美国生活,人见人憎、毅然地挺身出来作证人.但是他却是一个典型的美国少年.汤姆&#12539. 我说童年就像一罐甜酒.不过人是要越变越好才是.他的经历告诉我们,他亲眼目睹了一场残忍的凶杀案,除了个儿长高了、《汤姆索亚历险记》简介,用冒险实现自己的理想、创造出一片属于自己的梦,血手魔王,懂事了,各干出了许多令人捧腹的妙事:《故事的主人公汤姆是个天真,竟施出诡计,回想起来你也会很开心.谁没有在小时候做过一件半件的傻事当你越长大,还自动奉上谢礼,人们以为他们淹死了、尝起来就越香,就只有一点点偶尔才会想起的甜蜜回忆.终有一天,他为了正义,活泼而又顽皮的典型美国少年,勇敢、他是一个淘气.你对世界的看法不同了.看来,能让我们找到的.汤姆早年失去双亲,正在教堂为他们举行丧礼.他和野孩子夏克,正直,汤姆也有值得我们学习的地方.在主日学校也是一个令老师头疼的问题儿童,同辈羡慕的英雄,人们以为他们已经淹死了.马克&#12539、他救了自己的女友.像汤姆被罚粉刷围墙,他们却回来参加自己的葬礼了、但是,被寄养在姨妈那里,不但使别的孩子心甘情愿代替他工作,也有自己的烦恼.它所描写的是一群以汤姆为首的几个少年惊险,时隔越久、为了摆脱繁重的课业和呆板的课堂、他们实现了自己的愿望.在为他们举行了葬礼时,而他们却躲在教堂的钟楼上偷听,他坏事做尽、指出了凶手.在山洞探险中、西班牙海上黑衣大盗.后来和夏克逃到荒岛去,创造出一片人生的晴空,他挺身而出.这些顽皮的举动,乐观.其实孩子的顽皮有时候正好体现了孩子的天真烂漫,不惜离家出走,有快乐、拯救那无辜的罪犯沫夫彼得. 这种童真过了孩童时代就很难再寻,得到了一个活活饿死在山洞里的下场二.他们天真烂漫.但最后他还是恶有恶报,成为一个长辈喜欢.我相信;索亚是一个多面性的人物.千万不能像历险记里的那个心狠手辣的坏蛋卓伊一样.汤姆&#12539,即使你的童年再艰苦,用冒险改变自己的环境,只要有理想:聪明.在冒险中,你就会越觉得这些傻事有趣.这个栩栩如生的顽童形象,越让人回味,他的世界充满阳光和希望,去当绿林好汉,人的思想也在长,还让凶手落了网.在他们的头脑里;索亚的每一个故事,的确给我留下了深刻印象.墓地探险.并在顽皮之余,成为众人钦佩的小英雄,身子强壮了以外.他具有一般孩子没有的叛逆精神,美梦便会成真.逃到荒岛后,都令我浮想联翩,不再幼稚了,居然和夏克破获了一桩谋杀案.在姨妈眼中、捣蛋的孩子,我们的天性可以自由地发挥;吐温先生笔下的这个小机灵鬼既有自己的理想抱负
提问者采纳
do a lot of changes. He is a naughty boy, honest, also let the murderer, have dreams will come true, interesting in American life. It&#39: intelligent, Marine overlord blood hand sovereign is their yearning title, early lost in my aunt in the eyes, become all admired the little hero, besides height, and they hid in the church tower on eavesdropping. Don&#39.People always growSecond. In the monkey, the sea-shore left a deep impression to me. Later on. I said childhood is like a pot liqueur, sensible. They childlike, &quot. In the funeral for them when they cames thoughts are long. He and wild child shark. Like Tom was painted walls, he pointed out that the murderer. He witnessed the adventure of a brutal murder the innocent victim in coming up, people see people, it will be remembered the occasional sweet memory, positive and courageous, Tom, but no childish. He&#39.Actually the naughty children sometimes just reflected the innocent children, but a trick:The hero of the story is a naive. In spelunking. Who was done in one half of the foolish when you grow up, although cannot give us a model. It is also worth of learning, body has grown stronger, with adventure to realize my ideal. Fled to the island, also be XieLi. This vivid image, more pure, people thought they had drowned, to stand out and save the testimony of innocent criminals in Cardiff Peter, as long as have ideal, use adventure change their own environment. In Spain. Ts a typical American teenager, even if your childhood again hard, ant like adventure in the ruthless villains, black sea. He is not rebellious spirit, shark, people think they are drowned, create their own dreams, got a starved to death in the cave. Your view on the world is different, lively and Tom and naughty typical American teenager, but he resolutely to justice, a Sunday school teacher is the problem. And foam, when the group in their mind, we nature can freely, people&#39.Tom Sawyer is Shared a mus Shared this smart boy both their ambition, not only make the other children to replace him willingly. But he&#39, and his quest do bad. This childlike had childhood is hard to find, can let us find only a bit, they achieve their wishes, the aftertaste letting a person. I believe, the sweet taste.Tom was foster parents, he saved his girlfriend. One is getting better, in retrospect, happy, for they held a funeral church, you will feel these stupid, also have their own troubles, my graveyard to extrapolate. Someday, after the longer. But he still las toman, in order to get rid of heavy schoolwork and inflexible classroom. Mark Twain sir&#39, create a life of the sky. In risk, liks about a group led by Tom several young breathtaking, aunt: children at risk, becs experience tells us, to run away from home.Tom Sawyer gate every story, incredibly mischievous and sharTom Sawyer -- introduction, you will be happy, he is full of sunshine and the hope of the world
其他类似问题
为您推荐:
您可能关注的推广
英文的相关知识
其他15条回答
&Adventures of Tom Sawyer,& Introduction:
&Tom is the story of a naive character, lively and naughty typical American teenager. He and Wild Child Shack, the stem is laugh a lot of wonderful things. Like Tom to paint the walls a fine, the trick has facilities not only other children willing to work to replace him, but also automatically offer Xie. Shack later and fled to a desert island, it is thought that they drowned, was held at the church for their funeral, and they are hiding in the church bell tower on eavesdropping . those naughty moves, although we can not give a model, but he to justice, determined to be a witness to come forward, and save it ...
二、《汤姆索亚历险记》简介:
Second, &Tom Rope Asia Experiences dangers Records& introduces:
《故事的主人公汤姆是个天真,活泼而又顽皮的典型美国少年.他和野孩子夏克,各干出了许多令人捧腹的妙事.像汤姆被罚粉刷围墙,竟施出诡计,不但使别的孩子心甘情愿代替他工作,还自动奉上谢礼.后来和夏克逃到荒岛去,人们以为他们淹死了,正在教堂为他们举行丧礼,而他们却躲在教堂的钟楼上偷听.这些顽皮的举动,虽然不能给我们做模范、但是,他为了正义、毅然地挺身出来作证人、拯救那无辜的罪犯沫夫彼得.并在顽皮之余,居然和夏克破获了一桩谋杀案,成为众人钦佩的小英雄.看来,汤姆也有值得我们学习的地方.
&story leading character Tom is naive, lively and mischievous model American youth. He and the urc...
你演的《奋斗》很帅气,,很喜欢你
翻译不出,太长了~
太喜欢你了,希望你能给大家带来更多好的作品!
谁演的奋斗呀!怎么那么晕呢 不是翻译吗?怎么整出演员了?
最近才喜欢上文章的,好帅!
挺喜欢你演的向南的,,出神入化
usy uhshwuh hsajhd- SIDhd '
hsajdhjhudjwdvdtd
tHWEDjkKAJSKDU Uijhdjshduyd SJDHJADjsdhjd shJ aHZSdsy
HSDjhsdjwuDHhjshdugux jzxkjsaudq shudsaywu asjdjadskdwu ahsjadhu
Dhfghsus hjhduHDgahdxjszhd=
hdashdjsah saj sadjiw deakjds as dasda sddksjdka disjdau ed
dshajsdf eu s x cndc sid fe hjszhheufdhushdjh f hjf dshfj ashfuew jsdjs dhajs dyw djahds sdhsj deyudhzjcdshfasfku hf uhe szhdsuhduw dzsc cbud sbua dhudbwdhushd hwud
a sjhdu a d wd hudh
People always grow, besides height, body has grown stronger, people's thoughts are long. Your view on the world is different, sensible, but no childish. One is getting better. Don't like...
Two, &Tom Suoya adventure & brief introductions:
The &story leading character in a novel Tom is innocent and lively, and naughty as well representative USA teenager. He composes in reply open space child Xia Ke , every does the wonderful thing having put up many making one burst out laughing. Whitewash an enclosing wall , execute out crafty plot unexpectedly like Tom is punished, not only making other child be willing to replace him to work, the automation has the honor to present Xie Li . Compose in reply Xia Ke afterwards escaping going to uninhabited island, people believes they have been drowned , church is that they hold a funeral in course of...
我们同学一致认为你开车狂唱(爱如潮水)那一块最势大了 ,挺帅的
Second, &Tom Sawyer -- introduction:
The hero of the story is a naive, lively and Tom and naughty typical American teenager. He and wild child shark, do a lot of changes. Like Tom was painted walls, but a trick, not only make the other children to replace him willingly, also be XieLi. Later on, and fled to the island, shark, people think they are drowned, for they held a funeral church, and they hid in the church tower on eavesdropping. These naughty action, although cannot give us a model, but he resolutely to justice, to stand out and save the testimony of innocent criminals in Cardiff Peter. And foam, incredibly mischievous and shark uncovered a m...
最近才喜欢上文章的好帅!
最近才喜欢上文章的,好帅!
Second, &Tom Sawyer -- introduction:
The hero of the story is a naive, lively and Tom and naughty typical American teenager. He and wild child shark, do a lot of changes. Like Tom was painted walls, but a trick, not only make the other children to replace him willingly, also be XieLi. Later on, and fled to the island, shark, people think they are drowned, for they held a funeral church, and they hid in the church tower on eavesdropping. These naughty action, although cannot give us a model, but he resolutely to justice, to stand out and save the testimony of innocent criminals in Cardiff Peter. And foam, incredibly mischievous and shark uncovered a mu...
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁查看: 3163|回复: 36
Google 1月 Onsite 面经
精华主题学分
中级农民-加分请看右边栏-多参与|分享|记录|反馈, 积分 174, 距离下一级还需 326 积分
在线时间 小时
月) 码农类 硕士 全职@Google - 内推 - Onsite&|Failfresh grad应届毕业生
注册一亩三分地论坛,查看更多干货!
才可以下载或查看,没有帐号?
round 1: 黑白棋。给一个棋盘和一个棋子的坐标,判断这个棋子是不是活的。Leetcode也有类似的比这个难得题目。DFS/BFS看能不能走到棋盘的边缘就好。分析复杂度。给一堆点,问怎么画凸包,说思路就好。这个面经上没见过,lz当时是完全不知道凸包的概念。
round 2: 写有weight的随机数生成器,请参见以前的面经。写完之后问了如何测试。给一个int array,和一个这个array里面存在的数字,把这个数移到array的最后面。two pointers就好。
round 3: 给一个int array,找任意一个popular number, popular number就是出现次数大于等于array length 的 1/4 的数。其实就是 Leetcode 169, 229 Mojority Element. 第一问unsorted array 用的hashmap。 第二问sorted array用的binary search。lz没有说moore's voting algorithm的做法感觉有点假。第二问复杂度,worst case问的很细。. more info
round 4: 类似Leetcode 26, 80, remove adjacent duplicate elements from a list of characters。类似Lint code data stream median, 写个API,有两个方法,addURL(String url) 和 getURL(),getURL()返回的是目前为止所有URL长度的中位数。lz使用两个heap做的。follow-up: what if we know the range of the input,比如我们知道URL的大小不会超过2k,那有没有别的implement的方法。这个没想出来请大家指教。
<p id="rate_268" onmouseover="showTip(this)" tip="谢谢你的介绍!&大米 + 3 升
" class="mtn mbn">
本帖被以下淘专辑推荐:
& |主题: 42, 订阅: 43
精华主题学分
在线时间 小时
凸包我回来还没仔细查,有weight随机数题目和代码如下:
. 1point 3acres 璁哄潧
import java.util.A
import java.util.A
import java.util.R. 鍥磋鎴戜滑@1point 3 acres
-google 1point3acres
大概就是一个数组
1,4,2,6....
每次调用一个函数,按照数组里面的数字的大小,返回相应的Index。
比如, 上面的例子就是
1/13 的概率返回0,
4/13的概率返回1.
说了两个办法,一个累加起来, 然后用一个随即数,看看在哪个范围里面。另一个先加好,然后Binary search,让写了第二个。
大概写了5分钟,然后改成了一个class,
因为题目有什么城市阿,人口阿乱七八糟的。 又华了5分钟,然后没时间了。
public class Q099_Random_Number_Generator_With_Probality {.
& & & & public static void main(String[] args) {
& & & & & & & & int[] nums = {1, 4, 2, 6};
& & & & & & & & MyRNG r0 = new MyRNG(nums); //
& & & & & & & & MyRNG2 r = new MyRNG2(nums);
& & & & & & & & System.out.println(r.nextInt());
& & & & & & & &
. From 1point 3acres bbs
// O(nums.length) space, O(logn) time
class MyRNG2 extends Random{
& & & & int[]
& & & & Random r = new Random();. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
& & & & public MyRNG2(int[] nums) {
& & & & & & & & probs = new int[nums.length];
& & & & & & & & probs[0] = nums[0];. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
& & & & & & & & for (int i = 1; i & nums. i++) {
& & & & & & & & & & & & probs = probs[i - 1] + nums;.鏈枃鍘熷垱鑷1point3acres璁哄潧
& & & & & & & & }
& & & & & & & & System.out.println(Arrays.toString(probs));
& & & & . from: /bbs
& & & & @Override
& & & & public int nextInt() {-google 1point3acres
& & & & & & & & int target = r.nextInt(probs[probs.length - 1] + 1);
& & & & & & & & return search(probs, target);
& & & & }. /bbs
& & & & private int search(int[] nums, int target) {
& & & & & & & & int start = 0, end = nums.length - 1;
& & & & & & & & while (start &= end) {
& & & & & & & & & & & & int mid = start + (end - start) / 2;
& & & & & & & & & & & & if (nums[mid] &= target) {
& & & & & & & & & & & & & & & & end = mid - 1;
& & & & & & & & & & & & } else {
& & & & & & & & & & & & & & & & start = mid + 1;
& & & & & & & & & & & & }. 1point 3acres 璁哄潧
& & & & & & & & }
& & & & & & & &
& & & & & & & & return end + 1;
// O(cum) space, O(1) time
class MyRNG extends Random{
& & & & int[]. Waral 鍗氬鏈夋洿澶氭枃绔,
& & & & Random r = new Random();.1point3acres缃
& & & & public MyRNG(int[] nums) {. /bbs
& & & & & & & & int sum = 0;
& & & & & & & & for (int i = 0; i & nums. i++) {
& & & & & & & & & & & & sum += nums;
& & & & & & & & }
& & & & & & & & probs = new int[sum];. Waral 鍗氬鏈夋洿澶氭枃绔,
& & & & & & & & int index = -1;
& & & & & & & & for (int i = 0; i & nums. i++) {
& & & & & & & & & & & & int count = 0; 鏉ユ簮涓浜.涓夊垎鍦拌鍧.
& & & & & & & & & & & & while (count++ & nums) {. 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
& & & & & & & & & & & & & & & & probs[++index] =.鏈枃鍘熷垱鑷1point3acres璁哄潧
& & & & & & & & & & & & }
& & & & & & & & }. from: /bbs
& & & & & & & & System.out.println(Arrays.toString(probs));
& & & & . 鐗涗汉浜戦泦,涓浜╀笁鍒嗗湴
& & & & @Override
& & & & public int nextInt() {
& & & & & & & & int index = r.nextInt(probs.length);.
& & & & & & & & return probs[index];
精华主题学分
在线时间 小时
最后一题可以用Binary Index Tree,add和get操作都是log(n)
精华主题学分
在线时间 小时
类似Lint code data stream median, 写个API,有两个方法,addURL(String url) 和 getURL(),getURL()返回的是目前为止所有URL长度的中位数。lz使用两个heap做的。follow-up: what if we know the range of the input,比如我们知道URL的大小不会超过2k,那有没有别的implement的方法。这个没想出来请大家指教。. 涓浜-涓夊垎-鍦帮紝鐙鍙戝竷
感觉可以用一个2k的数组,比如a[i]记录长度小于等于i的URL的个数,然后用二分来找中位数,这样行不?
精华主题学分
在线时间 小时
请问LZ是在哪里面的?
精华主题学分
在线时间 小时
请问LZ是在哪里面的?.鏈枃鍘熷垱鑷1point3acres璁哄潧
精华主题学分
在线时间 小时
请问是多久以前啊?
精华主题学分
在线时间 小时
同意楼主,我也觉得moore's voting algorithm有点假,不做题根本不会知道
精华主题学分
在线时间 小时
感觉楼主面的可以啊,feedback咋说
精华主题学分
在线时间 小时
请问是多久以前啊?. /bbs
1月20号面的
精华主题学分
在线时间 小时
最后面第二个题就是bucket sort, 另外求问凸包和weight随机数的详情和解法...
精华主题学分
在线时间 小时
最后面第二个题就是bucket sort, 另外求问凸包和weight随机数的详情和解法...
凸包我回来还没仔细查,有weight随机数题目和代码如下:
import java.util.A
import java.util.R&div&/**&/div&&div&&/div&&div&大概就是一个数组&/div&&div&1,4,2,6....&/div&&div&每次调用一个函数,按照数组里面的数字的大小,返回相应的Index。&/div&&div&比如, 上面的例子就是&/div&&div&1/13 的概率返回0,&/div&&div&4/13的概率返回1.&/div&&div&说了两个办法,一个累加起来, 然后用一个随即数,看看在哪个范围里面。另一个先加好,然后Binary search,让写了第二个。&/div&&div&大概写了5分钟,然后改成了一个class,&/div&&div&因为题目有什么城市阿,人口阿乱七八糟的。 又华了5分钟,然后没时间了。&/div&&div& */&/div&&div&public class Q099_Random_Number_Generator_With_Probality {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&public static void main(String[] args) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int[] nums = {1, 4, 2, 6};&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&MyRNG r0 = new MyRNG(nums); // &/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&MyRNG2 r = new MyRNG2(nums);&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&System.out.println(r.nextInt());&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&}&/div&&div&
&/div&&div&// O(nums.length) space, O(logn) time&/div&&div&class MyRNG2 extends Random{&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&int[]&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&Random r = new Random();&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&public MyRNG2(int[] nums) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&probs = new int[nums.length];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&probs[0] = nums[0];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&for (int i = 1; i & nums. i++) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&probs[i] = probs[i - 1] + nums[i];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&System.out.println(Arrays.toString(probs));&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&@Override &/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&public int nextInt() {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int target = r.nextInt(probs[probs.length - 1] + 1);&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&return search(probs, target);&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&private int search(int[] nums, int target) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int start = 0, end = nums.length - 1;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&while (start &= end) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&int mid = start + (end - start) / 2;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&if (nums[mid] &= target) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & & & & & &/span&end = mid - 1;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&} else {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & & & & & &/span&start = mid + 1;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&return end + 1;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&}&/div&&div&
&/div&&div&// O(cum) space, O(1) time&/div&&div&class MyRNG extends Random{&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&int[]&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&Random r = new Random();&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&public MyRNG(int[] nums) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int sum = 0;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&for (int i = 0; i & nums. i++) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&sum += nums[i];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&probs = new int[sum];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int index = -1;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&for (int i = 0; i & nums. i++) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&int count = 0;&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&while (count++ & nums[i]) {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & & & & & &/span&probs[++index] =&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&System.out.println(Arrays.toString(probs));&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&@Override &/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&public int nextInt() {&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&int index = r.nextInt(probs.length);&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & & & & & &/span&return probs[index];&/div&&div&&span class=&Apple-tab-span& style=&white-space:pre&&& & & & &/span&}&/div&&div&}&/div&复制代码
补充内容 ( 12:49):
啊抱歉格式怎么变成了这样。。。可以删一下吗。。。
精华主题学分
在线时间 小时
楼主跪在哪里?你不是都做出来了吗?
精华主题学分
在线时间 小时
能麻烦LZ讲一下round 3的sorted array是怎么用binary search的呀?没想出lgN的解法。。
精华主题学分
在线时间 小时
-google 1point3acres
能麻烦LZ讲一下round 3的sorted array是怎么用binary search的呀?没想出lgN的解法。。
一个思路:分成8段,看那些段的首尾相同。对于首尾相同的,再用binary search依次找到该数出现的频率,取频率大于1/4那个
精华主题学分
在线时间 小时
import java.util.A
import java.util.R
好的,谢谢啦
精华主题学分
在线时间 小时
能麻烦LZ讲一下round 3的sorted array是怎么用binary search的呀?没想出lgN的解法。。. more info
n/4的话只需要看0, n/4, n/2, n*3/4, n这几个点相邻的是否相等,相等那个数就是popular number
补充内容 ( 14:48):
好像不对,请忽略我
精华主题学分
在线时间 小时
n/4的话只需要看0, n/4, n/2, n*3/4, n这几个点相邻的是否相等,相等那个数就是popular number
鏉ユ簮涓浜.涓夊垎鍦拌鍧.
补充内容 ....鏈枃鍘熷垱鑷1point3acres璁哄潧
如果是 sorted, 这样搞?
. From 1point 3acres bbs
(1)看 arr[0]和 arr[n/4]是否相等,相等则返回arr[0];-google 1point3acres
(2)如果不等, binary search arr[n/4]第一次出现的位置,假设为j,比较 arr[j] 和 arr[j+n/4],相等则返回 arr[j]
(3) 如果上一步还好是不返回,接着binary search [n/2].... 鍥磋鎴戜滑@1point 3 acres
(4) 类似的接着binary search [n/4]. from: /bbs
补充内容 ( 15:55):
第三步是 搜索 arr[n/2]
第四部是搜索 arr[3n/4]
最坏的情况是3次 binary search, 4次comparison.
精华主题学分
在线时间 小时
如果是 sorted, 这样搞?
(1)看 arr[0]和 arr[n/4]是否相等,相等则返回arr[0];. From 1point 3acres bbs
楼上有个说法对了,分成八段找相同的,n/4必定会cover一个n/8的区域,然后再对那个数search range,两次binary search 搞定
精华主题学分
在线时间 小时
一个思路:分成8段,看那些段的首尾相同。对于首尾相同的,再用binary search依次找到该数出现的频率,取 ...
有一定道理!我也觉得先确定几个数,再分别用binary search去找精确区间是合理的。
补充内容 ( 15:40):
细想了一下,貌似check 0, 1/4, 1/2, 3/4, 1这五个点对应的range就可以了,必有一个是落在1/4 majority里面的
<form method="post" autocomplete="off" id="fastpostform" action="forum.php?mod=post&action=reply&fid=145&tid=168330&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost"
onSubmit="
// TODO Howard 11/3/2015
var sbtn = $('fastpostsubmit');
sbtn.disabled =
sbtn.innerHTML = ' 回复发表中... ';
sbtn.setAttribute('background', sbtn.style.background);
sbtn.setAttribute('bordercolor', sbtn.style.borderColor);
sbtn.style.background = '#C7C7C7';
sbtn.style.borderColor = '#8B8B8B';
var form =
// --product--
var isValid = fastpostvalidate(form, null, 0);
if(!isValid) reoverBtn();
return isV
// --product--
// --testing--
//setTimeout(function() {
// var isValid = fastpostvalidate(form, null, 0);
// if(!isValid) reoverBtn();
//}, 2000);
// --testing--
您需要登录后才可以回帖
回帖并转播
回帖后跳转到最后一页
Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!
一亩三分地推荐 /5
地主Warald亲手做你的申请,针对你的背景和目标,考虑申请、学习、就业、移民等系列问题,制定申请策略。
“offer”指全额奖学金,免学费全免+每月工资,Berkeley, CMU, JHU, UIUC, Gatech, UMich, UCLA, Columbia,欢迎观赏。
电子工程、计算机、统计、金数金工、化工等, Stanford, Berkeley, CMU, Cornell, Yale, Columbia, Chicago, Duke, UPenn, UIUC, Brown, UMich, JHU等
有留学、申请、找工、职业规划上的难题?先上论坛提问!
论坛考古也帮不上忙,发帖得到的回答仍然不够?电话找Warald来解答!
WARALD新书上市啦:《你不知道的美国留学》清华大学出版社,各大电商发售
Powered by

我要回帖

更多关于 amp quot 的文章

 

随机推荐