注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 sagaGSXK 于 2022-2-9 18:11 编辑
Code Signal 4道题
1. first and last: 输入: a list of str [a*****b, c*****d,e****f,... x*****z],要求返回的字符串中包含 a的首字母与a[i+1]的末尾字母 (对于a[n-1],返回a[0]的末尾字母),
[a***d,c***f,...,x***b]
Solution:
2. WDLDL: 给定字符串,parse with the given rule (1. 若有W在str中,在output append W, 2. 若有D,append D,3. 若有L,appendL,按照序号循环执行)
Solution:
3.User Message:给定Messages,Userwidth, Width,输出按照格式parse的信息,要求:单行最多只能Width宽,且单行最多显示Userwidth个字符,其余部分space补全,如果User1发消息则靠左显示信息,User则靠右
Solution:苦力题,完全按照只是操作,可以先把message split to list of words, 然后用逻辑去决定是否换行,注意词语间的空格也要计算width
4.Cyclic Shift,输入:list of numbers,判断其中不同number是否为其余number 的 Cyclic Shift,输出总的cyclic Shift 对数
example:
输入:[123,231,321,0456,6045,123]
输出:4
解释:123,231,123两两构成Cyclic Shift,共3对,321不与任何元素构成Shift,0456与6045构成1对Shift.
Solution:
Use hashmap to store all cyclic Shift
When reading an element, check the hashmap with every rotation of the word
If there is such key, add the element to the key value of its rotation (I use defaultdict
)
Else, dict[element] = [element]
总体来说很简单2Easy2Medium,似乎比较注重字符串处理
|