最近是财报密集日,昨天聊完 JD,今天来聊聊鹅厂。
5 月 14 日,腾讯发布了一季度财报,2025 年一季度营收 1800 亿,同比增长 13%,净利润 478 亿,增长 14%。
又是营收和利润双增的一个季度,这就是腾讯,可以锁仓十年的腾讯。
如果你觉得双增还不够说服力,那我可以再分享一些其他数据。
根据这份一季度财报,腾讯目前的毛利率是 56%(核心成本是服务器、带宽和研发费用),目前是连续十个季度实现"经营利润增速>毛利增速>收入增速"的高质量增长模式。这在中概互联网行业里,是独一家。
除了财务方面,还有应用方面的数据也有不少亮点。其中最为突出的,是微信和 WeChat 合并月活用户数首次突破 14 亿。
截止 3 月底,微信和 WeChat 合并月活用户数为 14.02 亿,同比增长 3%。
由于数据上合并了 WeChat,因此会有不少非大陆用户数据,但占比不会太高,截止 2024 年末,中国大陆的人口数量为 14.08 亿。除去一些新生儿和不太会用智能手机的老人,中国人均使用微信已经从"统计数据"上完全实现。
这或许是首个真正意义上的"国民级"应用。
对此,你怎么看?欢迎评论区交流。
...
回归主题。
来一道和「腾讯 - 社招」相关的算法题。
平台:LeetCode
题号:2013
给你一个在 X-Y
平面上的点构成的数据流。设计一个满足下述要求的算法:
轴对齐正方形是一个正方形,除四条边长度相同外,还满足每条边都与 x-轴
或 y-轴
平行或垂直。
实现 DetectSquares
类:
DetectSquares()
使用空数据结构初始化对象void add(int[] point)
向数据结构添加一个新的点 point = [x, y]
int count(int[] point)
统计按上述方式与点 point = [x, y]
共同构造 轴对齐正方形 的方案数。示例:
输入:
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
输出:
[null, null, null, null, 1, 0, null, 2]
解释:
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // 返回 1 。你可以选择:
// - 第一个,第二个,和第三个点
detectSquares.count([14, 8]); // 返回 0 。查询点无法与数据结构中的这些点构成正方形。
detectSquares.add([11, 2]); // 允许添加重复的点。
detectSquares.count([11, 10]); // 返回 2 。你可以选择:
// - 第一个,第二个,和第三个点
// - 第一个,第三个,和第四个点
提示:
add
和 count
的总次数最多为 对于 add
操作,我们可以使用「哈希表 套 哈希表」的方式,以 {x, {y : 点 (x,y) 数量}}
的形式对传入点进行存储。
对于 count
查询而言,假定传入的点为
,我们可以先查询
行都有哪些列,枚举这些列( 即枚举点
),由
和
可得正方形边长
,此时再检查唯一确定的两点
和
的出现次数,应用乘法原理,即可知道该正方形的方案数,统计所有合法方案数即是该询问的答案。
利用题目范围给定的 x
和 y
具有明确的范围 0 <= x, y <= 1000
,我们可以使用数组充当哈希表,但是为了拓展性和减少边界判断,即支持将平面拓展到任意大小,最好还是直接使用哈希表。
Java 代码:
class DetectSquares {
Map<Integer, Map<Integer, Integer>> row2Col = new HashMap<>();
public void add(int[] point) {
int x = point[0], y = point[1];
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
col2Cnt.put(y, col2Cnt.getOrDefault(y, 0) + 1);
row2Col.put(x, col2Cnt);
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
for (int ny : col2Cnt.keySet()) {
if (ny == y) continue;
int c1 = col2Cnt.get(ny);
int len = y - ny;
int[] nums = newint[]{x + len, x - len};
for (int nx : nums) {
Map<Integer, Integer> temp = row2Col.getOrDefault(nx, new HashMap<>());
int c2 = temp.getOrDefault(y, 0), c3 = temp.getOrDefault(ny, 0);
ans += c1 * c2 * c3;
}
}
return ans;
}
}
C++ 代码:
class DetectSquares {
public:
unordered_map<int, unordered_map<int, int>> row2Col;
DetectSquares() {}
void add(vector<int> point) {
int x = point[0], y = point[1];
row2Col[x][y]++;
}
int count(vector<int> point) {
int x = point[0], y = point[1];
int ans = 0;
if (row2Col.find(x) == row2Col.end()) return0;
auto& col2Cnt = row2Col[x];
for (auto& entry : col2Cnt) {
int ny = entry.first;
if (ny == y) continue;
int c1 = entry.second;
int len = y - ny;
vector<int> nxOptions = {x + len, x - len};
for (int nx : nxOptions) {
if (row2Col.find(nx) == row2Col.end()) continue;
auto& temp = row2Col[nx];
int c2 = (temp.find(y) != temp.end()) ? temp[y] : 0;
int c3 = (temp.find(ny) != temp.end()) ? temp[ny] : 0;
ans += c1 * c2 * c3;
}
}
return ans;
}
};
Python 代码:
class DetectSquares:
def __init__(self):
self.row2Col = defaultdict(lambda: defaultdict(int))
def add(self, point: List[int]) -> None:
x, y = point
self.row2Col[x][y] += 1
def count(self, point: List[int]) -> int:
x, y = point
ans = 0
col2Cnt = self.row2Col.get(x, {})
for ny in col2Cnt:
if ny == y:
continue
c1 = col2Cnt[ny]
length = y - ny
for nx in [x + length, x - length]:
temp = self.row2Col.get(nx, {})
c2 = temp.get(y, 0)
c3 = temp.get(ny, 0)
ans += c1 * c2 * c3
return ans
Java 代码(数组充当哈希表):
class DetectSquares {
int N = 1010;
int[][] cnts = newint[N][N];
public void add(int[] point) {
int x = point[0], y = point[1];
cnts[x][y]++;
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
for (int ny = 0; ny < N; ny++) {
if (y == ny) continue;
int c1 = cnts[x][ny];
if (c1 == 0) continue;
int len = y - ny;
int[] nums = newint[]{x + len, x - len};
for (int nx : nums) {
if (nx < 0 || nx >= N) continue;
int c2 = cnts[nx][y], c3 = cnts[nx][ny];
ans += c1 * c2 * c3;
}
}
return ans;
}
}
add
操作的复杂度为 ,count
最坏情况会扫描完所有此前加入的点,复杂度为
巨划算的 LeetCode 会员优惠通道目前仍可用 ~