裁员这事,最怕公司拿你当“快递”,今天通知,明天签字,赔偿还想往低了按。结果这位37岁同事偏不接招,20万没到位,人家就正常上班,打卡、领工资,一个月2.6万,硬是耗了8个月。你别说,这账算下来,是真不亏。
![]()
还有人问,字节都开4万了,为啥不走?评论区有人说得挺实在:你以为跳槽是加薪,到了新地方说不定先拿你祭天。还有人说,37岁这年纪,最值钱的不是冲,是稳。这个判断我认。
很多公司就赌你慌,赌你年纪上来就不敢拖。你一急,赔偿打折,主动权也没了。真能顶住压力、心态不崩、每天照常坐那儿,这不是赖着不走,这是把本来该你的东西,一分分拿回来。HR看着你准点打卡,估计血压都得上来。
最短补全词
车牌里一堆乱七八糟的字符,单词列表里一堆候选词,最后要找“最短补全词”。这题看着像字符串题,真写起来,很多人第一反应就跑偏了:要么想着排序,要么想着子串匹配。其实都不对。
这题我第一眼先看两个东西: 一个是车牌里真正有用的只有字母,数字和空格都该直接扔掉; 另一个是“补全”不是顺序匹配,是词频覆盖比如车牌里有两个s,那候选词里就得至少有两个s,少一个都不行。
所以这题别搞花活,老老实实上 26 位字母计数数组就行,快,而且不容易写歪。
先把车牌处理掉。大小写统一转小写,只统计字母出现次数:
privateint buildNeed(String licensePlate) {
int need = newint[26];
for (int i = 0; i < licensePlate.length; i++) {
char c = licensePlate.charAt(i);
if (Character.isLetter(c)) {
need[Character.toLowerCase(c) - 'a']++;
}
}
return need;
}
接着判断某个单词能不能覆盖这个需求。这里不要去想“包含哪些字符”,那样很容易漏掉重复字母。还是计数最稳:
privatebooleanmatch(String word, int[] need){
int cnt = newint[26];
for (int i = 0; i < word.length; i++) {
cnt[word.charAt(i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] < need[i]) {
returnfalse;
}
}
returntrue;
}
主流程就更直接了。遍历所有候选词,谁能补全就拿来和当前答案比长度,保留更短的那个。题目如果有多个长度一样的,按原始顺序返回第一个,所以这里只在“严格更短”时更新答案,不要手贱写成<=。
classSolution{
public String shortestCompletingWord(String licensePlate, String[] words){
int need = buildNeed(licensePlate);
String ans = ;
for (String word : words) {
if (match(word, need)) {
if (ans == || word.length < ans.length) {
ans = word;
}
}
}
return ans;
}
privateint buildNeed(String licensePlate) {
int need = newint[26];
for (int i = 0; i < licensePlate.length; i++) {
char c = licensePlate.charAt(i);
if (Character.isLetter(c)) {
need[Character.toLowerCase(c) - 'a']++;
}
}
return need;
}privatebooleanmatch(String word, int[] need){
int cnt = newint[26];
for (int i = 0; i < word.length; i++) {
cnt[word.charAt(i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] < need[i]) {
returnfalse;
}
}
returntrue;
}
}
拿个例子过一下:licensePlate = "1s3 PSt"提取字母后得到s, p, s, t,需求就是:s=2, p=1, t=1。 再去扫step、steps、stripe、stepple这些词,step少一个s,直接淘汰;steps刚好够,而且长度最短,答案就是它。
这题时间复杂度其实很老实。假设有n个单词,每个单词平均长度是m,那就是O(n * m)。因为 26 个字母的比较是常数级,基本不用太担心性能。面试里这题也不看你能不能卷出更玄学的写法,就看你有没有抓到“词频覆盖”这个点。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.