阿里巴巴 2025 届校招开始了,岗位 and 原题抢先看!

阿里 春招 启动

阿里巴巴正式启动春季 2025 届校园招聘,其中超过 70% 均为技术岗位。

目前放出来的技术岗位中,工作地点分布在 杭州、北京、上海 和 成都 四个城市。

其中杭州占比最多,共有 25 种技术岗位可投递(总的技术岗位种类数为 28 类),杭州覆盖率高达 89%。另外三座城市的情况分别是:北京(9)、上海(8)和 成都(1)。

这里重点贴一下与公主号读者相关性较高的岗位。

算法:

开发:

测开:

目前招聘岗位刚放出来,有投递意向的同学可以开始着手准备了。

但也有注意事项 ⚠️⚠️⚠️:切忌海投!!!

...

回归主线。

无论是算法、研发还是测开,只要是计算机相关岗位,在校招中最不能绕过的环节是:算法

因此今天来带大家做一道「阿里巴巴」去年(2024 届毕业生)校招中的面试真题。

题目描述

平台:LeetCode

题号:791

给定两个字符串 orders

order 的所有单词都是唯一的,并且以前按照一些自定义的顺序排序。

s 的字符进行置换,使其与排序的 order 相匹配。

更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x 也应该出现在 y 之前。

返回满足这个性质的 s 的任意排列 。

示例 1:

less 复制代码
输入: order = "cba", s = "abcd"

输出: "cbad"

解释: 
"a"、"b"、"c"是按顺序出现的,所以"a"、"b"、"c"的顺序应该是"c"、"b"、"a"。
因为"d"不是按顺序出现的,所以它可以在返回的字符串中的任何位置。"dcba"、"cdba"、"cbda"也是有效的输出。

示例 2:

ini 复制代码
输入: order = "cbafg", s = "abcd"

输出: "cbad"

提示:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = o r d e r . l e n g t h < = 26 1 <= order.length <= 26 </math>1<=order.length<=26
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = s . l e n g t h < = 200 1 <= s.length <= 200 </math>1<=s.length<=200
  • orders 由小写英文字母组成
  • order 中的所有字符都 不同

构造

根据题意进行模拟即可:起始先使用大小为 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = 26 C = 26 </math>C=26 的数组 cntss 的所有字符进行词频统计,随后根据 order 的优先级进行构造。

若字符 xorder 中排于 y 前面,则先往答案追加 cnts[x] 个字符 x,再往答案追加 cnts[y] 个字符 y,并更新对应词频,最后将仅出现在 s 中的字符追加到答案尾部。

Java 代码:

Java 复制代码
class Solution {
    public String customSortString(String order, String s) {
        int[] cnts = new int[26];
        for (char c : s.toCharArray()) cnts[c - 'a']++;
        StringBuilder sb = new StringBuilder();
        for (char c : order.toCharArray()) {
            while (cnts[c - 'a']-- > 0) sb.append(c);
        }
        for (int i = 0; i < 26; i++) {
            while (cnts[i]-- > 0) sb.append((char)(i + 'a'));
        }
        return sb.toString();
    }
}

C++ 代码:

C++ 复制代码
class Solution {
public:
    string customSortString(string order, string s) {
        vector<int> cnts(26, 0);
        for (char c : s) cnts[c - 'a']++;
        string result = "";
        for (char c : order) {
            while (cnts[c - 'a']-- > 0) result += c;
        }
        for (int i = 0; i < 26; i++) {
            while (cnts[i]-- > 0) result += (char)(i + 'a');
        }
        return result;
    }
};

Python 代码:

Python 复制代码
class Solution:
    def customSortString(self, order: str, s: str) -> str:
        cnts = [0] * 26
        for c in s:
            cnts[ord(c) - ord('a')] += 1
        ans = ''
        for c in order:
            num = ord(c) - ord('a')
            if cnts[num] > 0:
                ans += c * cnts[num]
                cnts[num] = 0
        for i in range(26):
            if cnts[i] > 0:
                ans += chr(i + ord('a')) * cnts[i]
        return ans

TypeScript 代码:

TypeScript 复制代码
function customSortString(order: string, s: string): string {
    const cnts = new Array<number>(26).fill(0)
    for (const c of s) cnts[c.charCodeAt(0) - 'a'.charCodeAt(0)]++
    let ans = ''
    for (const c of order) {
        while (cnts[c.charCodeAt(0) - 'a'.charCodeAt(0)]-- > 0) ans += c
    }
    for (let i = 0; i < 26; i++) {
        while (cnts[i]-- > 0) ans += String.fromCharCode(i + 'a'.charCodeAt(0));
    }
    return ans
}
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n + m ) O(n + m) </math>O(n+m)
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( C ) O(C) </math>O(C),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = 26 C = 26 </math>C=26 为字符集大小

我是宫水三叶,每天都会分享算法知识,并和大家聊聊近期的所见所闻。

欢迎关注,明天见。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
ss27318 分钟前
手写MyBatis第31弹-用工厂模式重构MyBatis的SqlSession创建过程
spring boot·后端·mybatis
Victor35640 分钟前
Redis(22) Redis的持久化机制有哪些?
后端
一个热爱生活的普通人40 分钟前
使用 Makefile 和 Docker 简化你的 Go 服务部署流程
后端·go
Victor35641 分钟前
Redis(23) RDB和AOF有什么区别?
后端
盛夏绽放7 小时前
jQuery 知识点复习总览
前端·javascript·jquery
hui函数8 小时前
Flask电影投票系统全解析
后端·python·flask
胡gh9 小时前
依旧性能优化,如何在浅比较上做文章,memo 满天飞,谁在裸奔?
前端·react.js·面试
在未来等你9 小时前
Redis面试精讲 Day 27:Redis 7.0/8.0新特性深度解析
数据库·redis·缓存·面试
大怪v9 小时前
超赞👍!优秀前端佬的电子布洛芬技术网站!
前端·javascript·vue.js
胡gh9 小时前
你一般用哪些状态管理库?别担心,Zustand和Redux就能说个10分钟
前端·面试·node.js