阿里巴巴 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 为字符集大小

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

欢迎关注,明天见。

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

相关推荐
码农小野3 小时前
基于SpringBoot的自习室预订系统
java·spring boot·后端
ac-er88884 小时前
如何在Flask中实现国际化和本地化
后端·python·flask
Adolf_19934 小时前
Flask-WTF的使用
后端·python·flask
DT——5 小时前
Vite项目中eslint的简单配置
前端·javascript·代码规范
mingzhi615 小时前
网安面试会问到的:http的长连接和短连接
http·面试·职场和发展
极客先躯6 小时前
高级java每日一道面试题-2024年9月16日-框架篇-Spring MVC和Struts的区别是什么?
java·spring·面试·mvc·struts2·框架篇·高级java
学习ing小白7 小时前
JavaWeb - 5 - 前端工程化
前端·elementui·vue
真的很上进7 小时前
【Git必看系列】—— Git巨好用的神器之git stash篇
java·前端·javascript·数据结构·git·react.js
胖虎哥er7 小时前
Html&Css 基础总结(基础好了才是最能打的)三
前端·css·html
qq_278063717 小时前
css scrollbar-width: none 隐藏默认滚动条
开发语言·前端·javascript