查看: 2901| 回复: 14
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第四轮】3.30-4.5 Career Cup 1.5

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
1.5 Implement a method to perform basic string compression using the counts
of repeated characters. For example, the string aabcccccaaa would become
a2blc5a3. If the "compressed" string would not become smaller than the original
string, your method should return the original string.

请参加活动的童鞋跟帖回复自己的解法,回复请参考以下格式:

【解题思路】
【时间复杂度】
【空间复杂度】
【gist link]
【test case】(optional,如果觉得比较好,欢迎贴出来分享)

Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎探讨各种follow up questions,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改



上一篇:【第四轮】3.30- 4.5 Career Cup 1.4
下一篇:【第四轮】3.30 - 4.5 Career Cup 1.6
推荐
mything 2015-4-4 13:17:50 | 只看该作者
全局:
【解题思路】
扫描输入,如果当前字母和之前字母相同,计数加一,否则重置计数器为1,之前字母设为当前字母
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link]
https://gist.github.com/n2iw/630917d5b1087c128380
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
                null <=  null ,
                "" <=  "" ,
                "a" <=  "a" ,
                "aa" <=  "aa" ,
                "ab" <=  "ab" ,
                "abc" <=  "abc" ,
                "aaaabc" <=  "aaaabc" ,
                "a4b2c1" <=  "aaaabbc" ,
                "a5b1c1" <=  "aaaaabc" ,
                "a5b1c2" <=  "aaaaabcc" ,

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

推荐
Godbless 2015-4-5 02:13:29 | 只看该作者
全局:
【解题思路】First thing is to see whether the compressed string will be larger than the original string. If so, return the original string. If not, traverse the string and count the unique characters and their frequency. concatenate them into anew string.
【时间复杂度】O(n+k^2) n is the length og the original string, k is the number of unique characters in the original string
【空间复杂度】O(k)
【gist link] https://github.com/StephenWeiXu/ ... blob/master/1_5.cpp
【test case】(optional,如果觉得比较好,欢迎贴出来分享)
https://github.com/StephenWeiXu/ ... blob/master/1_5.cpp

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

全局:
【解题思路】
* When we want to add something to a string constantly and without knowing the total length,
* StringBuilder is a good choice.
* We check whether the adjacent elements are same. if so, we increase count number; if not, we output the count number,
* new character and reset the count number to 1.
* Please be aware that at the end, we cannot detect the different adjacent elements, but we still need to add the count number.

【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/leonw007/ef69b87b8ec03c1a7d08
【test case】included in the above link

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
JamesJi 2015-3-31 23:28:31 | 只看该作者
全局:
【解题思路】
Use StringBuffer, first change the original string to CharArray[], then scan the whole array. New a res to store result. If there is a same, count ++, then append it to StringBuffer
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/JamesJi9277/f218761e65c2da766ef2

【test case】

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
laonong15 2015-4-1 03:54:53 | 只看该作者
全局:
【解题思路】
Brute force
* go through the String  to  count the repeat sequence and   put   char+count to new string
* compare the length of new string and  original string to decide which one  is returned
*
【时间复杂度】
O(n)
after read the book  solution find  this
"because string concatenation operates in 0(n^2)"
* so it is o(n^2)
【空间复杂度】
O(n)
【gist link]
https://gist.github.com/michaelniu/f154e37453aa1d7ebd91

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
iker01 2015-4-1 12:18:16 | 只看该作者
全局:
【解题思路】
use one iteration and a pre variable to compare the adjacent char
use a counter to record the same letter occurrence
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link]
python solution
https://gist.github.com/zhangjiang2013/acda7c7faf6726f4e5a5
【test case】(optional,如果觉得比较好,欢迎贴出来分享)

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
A30041839 2015-4-2 15:50:15 | 只看该作者
全局:
[solution]
process from left to right, count the number of adjacent chars and create the compressed string
[time]
O(n)
[space]
O(n)
[gist]
https://gist.github.com/A30041839/08722d22d49b05ef1ede

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
chongtianzs 2015-4-3 09:29:12 | 只看该作者
全局:
[solution]
扫一遍string,用一个count记录char出现的次数,用一个last记录之前的char,若相同count++,若不同保存之前的char和count,重置count。
[time]
O(n)
[space]
O(n)
[gist]
https://gist.github.com/chongtianzs/350a1064c730d885078b

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

地里匿名用户
🔗
匿名用户-DHKXZ  2015-4-3 11:08:00
【解题思路】
建立一个和原数组长度相同的新数组,然后遍历原数组里的值并按照题目规则插值给新数组,同时更新新数组index,每次插值前检查数组index是否即将溢出,如果溢出返回原数组。
【时间复杂度】
O(n)
【空间复杂度】
O(n)
【gist link】
https://gist.github.com/alikewmk ... mpresssamechar-java

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
slaink 2015-4-4 07:37:37 | 只看该作者
全局:
【解题思路】
/**
* Compress a string.
*
* Traverse through this string, if reach a character
* that is differ from its previous character, store
* previous character and its count, and put that new
* character into buffer, set counter to 1.
*/
【时间复杂度】  * Time complexity: O(n)
【空间复杂度】  * Space complexity: O(n)
【gist link] https://github.com/bxshi/intervi ... unt_compression.cpp
【test case】(optional,如果觉得比较好,欢迎贴出来分享)https://github.com/bxshi/intervi ... c/test/1_5_test.cpp

评分

参与人数 1大米 +7 收起 理由
pure0909 + 7 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
slaink 2015-4-4 07:37:49 | 只看该作者

。。。

全局:
本帖最后由 slaink 于 2015-4-4 08:18 编辑

网络太差又双🐔了请帮忙删除谢谢。。。
回复

使用道具 举报

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

本版积分规则

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