📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.5

🔗
sanguine 2014-6-25 21:44:18 | 只看该作者
全局:
readman 发表于 2014-6-25 21:33
input : abc;
output: a1b1c1;

没懂==我的输出结果也是abc,而不是a1b1c1啊……

我是先压缩,得到a1b1c1,然后判断a1b1c1长度大于abc,所以返回了abc

你这种方法先计算长度如果大于或等于的话直接返回结果,确实减少了很多操作,也不需要开辟新的O(n)的空间,但是同时,如果长度小于原字符串的话,不仅重新开辟了O(n)的空间,又重新traverse了一遍,这样复杂度虽然还是线性,但是不就变成2n了?

点评

不对矣,我看答案mydogismaotou输出的结果好像是m1y1d1o1g1i1s1m1a1o1t1o1u1,为什么会是这样?  发表于 2014-6-25 21:46
回复

使用道具 举报

🔗
readman 2014-6-25 21:46:26 | 只看该作者
全局:
sanguine 发表于 2014-6-25 21:44
没懂==我的输出结果也是abc,而不是a1b1c1啊……

我是先压缩,得到a1b1c1,然后判断a1b1c1长度大于abc ...

- = 良好的代码习惯是必要的.
题目明确写出要先检验是不是长于原始数据....

点评

I see, thx~ I will optimize my solution~  发表于 2014-6-25 22:09
回复

使用道具 举报

🔗
sanguine 2014-6-25 22:07:40 | 只看该作者
全局:
jaly50 发表于 2014-6-23 20:07
【解题思路】
从左向右数每个字符连续出现的个数,然后output.concat(s).concat(count(s)
如果output.len ...

Don't use String, using StringBuffer or StringBuilder instead!

if you use string,the time complexity will be O(n^2), because every time you update the character in the string, it will create a new string instead of modify on the original one

below is the open source code of concat() method:
  1.   public String concat(String str) {       
  2. int otherLen = str.length();       
  3. if (otherLen == 0) {            
  4. return this;
  5.         }
  6.         char buf[] = new char[count + otherLen];
  7.         getChars(0, count, buf, 0);
  8.         str.getChars(0, otherLen, buf, count);
  9.         return new String(0, count + otherLen, buf);
  10.     }
复制代码

点评

soga...谢了!  发表于 2014-6-27 16:47
回复

使用道具 举报

🔗
心焰 2014-6-25 22:50:02 | 只看该作者
全局:

【解题思路】
Use a string buffer to construct the compressed string. iterate the original string, if current char is the same as the previous one, increase the counter; otherwise, append previous char and its counter, reset current char and counter. Finally, compare the length of constructed string with the original one
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://github.com/FinalF/Carrer ... ingCompression.java
回复

使用道具 举报

🔗
心焰 2014-6-25 22:50:10 | 只看该作者
全局:

【解题思路】
Use a string buffer to construct the compressed string. iterate the original string, if current char is the same as the previous one, increase the counter; otherwise, append previous char and its counter, reset current char and counter. Finally, compare the length of constructed string with the original one
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://github.com/FinalF/Carrer ... ingCompression.java
回复

使用道具 举报

🔗
sanguine 2014-6-25 23:23:55 | 只看该作者
全局:
solution 1
Traverse each character in the string, compare the current char with the previous one, if equals, count++, otherwise, appends the previous char and count into the StringBuilder
[count: the occurrence of the same consecutive characters]
Time Complexity: O(n)
Space Complexity: O(n)
Tip: Here, we must use StringBuilder or StringBuffer rather than String. Because the String is immutable, every time we add a character into the string, it will create a new String and eventually the Time Complexity will be O(n^2)

solution 2
The same idea with the first solution
Improve: Instead check the length at the end, in this method, Scan first to count the length of the Compress String, if the length is large than the original, return original. In this way, do not need extra O(n) space to build the Compress String. Otherwise, more O(n) time needed to build Compress String, and the time Complexity will be O(2n)
Instead of using the StringBuilder, we use char[] to implements this method.
Time Complexity: O(n)
Space Complexity: O(1) or O(n)

The Code Link: http://www.jyuan92.com/post-316
回复

使用道具 举报

🔗
林微熙 2014-6-26 01:32:57 | 只看该作者
全局:
sanguine 发表于 2014-6-25 06:44
没懂==我的输出结果也是abc,而不是a1b1c1啊……

我是先压缩,得到a1b1c1,然后判断a1b1c1长度大于abc ...

我好像两个都输出了
我回去研究一下
回复

使用道具 举报

🔗
atlas1017 2014-6-26 14:31:42 | 只看该作者
全局:
sanguine 发表于 2014-6-25 20:53
It's wrong……

the string aabcccccaaa would become a2blc5a3

Thx for advice but I ran the case you provided and the output was correct : )
回复

使用道具 举报

🔗
whiteflower 2014-7-4 20:16:03 | 只看该作者
全局:
【解题思路】
traverse the string and count duplicate characters
and put corresponding result into a new string
【时间复杂度】O(N^2)
【空间复杂度】O(N)
【gist link】https://gist.github.com/JoshuaTang/f5f92df4db35ca55fa6b
回复

使用道具 举报

🔗
pyemma 2014-7-6 12:39:16 | 只看该作者
全局:
【解题思路】This problem is still a pointer problem, use two pointer to record the start position and end position of a sequence of same characters, then append the character to the new string, as well as the number.
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】
https://gist.github.com/5448908bb0831ef19c8b.git
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
"";
"a";
"aa";
"aabb";
"aaabbbcc";
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表