当前位置:网站首页>8.31 Tencent interview
8.31 Tencent interview
2022-07-07 23:28:00 【codepig16】
8.31 Tencent interview
Two arithmetic questions
Enter a set of numbers , Output the second largest number . Avoid using the sorting function that comes with the language
requirement : Use one-time traversal to realize
Thought analysis
Find the first k Big element , We need to learn to maintain a small top stack to achieve , If the element is larger than the top of the heap, put it in the heap , Here is the second element, which is relatively simple .
Code implementation
public int getSecond(int[] nums) {
//a Is the maximum value. b Is the second largest value
int a = Integer.MIN_VALUE, b = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > a) {
b = a; a = nums[i];}
else if (nums[i] > b) b = nums[i];
}
return b;
}
Enter two integers n and m, From the sequence 1,2,3…….n Take a few numbers at random , Make the sum equal to m , Ask for a list of all possible combinations , Arrange from small to large
Thought analysis
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
// From index To n Middle sum is m Result
public void dfs(int index, int m, int n) {
if (m == 0) {
res.add(new ArrayList<>(list));
} else {
for (int i = index; i <= m && i <= n; i++) {
list.add(i);
dfs(i + 1, m - i, n);
list.remove(list.size() - 1);
}
}
}
The type of the second question can be changed into , Get the number of conditions from the array , Similar topics are
边栏推荐
- MySQL Index Optimization Practice II
- FreeLink开源呼叫中心设计思想
- FPGA basics catalog
- Vs extension tool notes
- 2021icpc Shanghai h.life is a game Kruskal reconstruction tree
- Coreseek:第二步建索引及測试
- [compilation principle] lexical analysis design and Implementation
- USB (十七)2022-04-15
- Force deduction solution summary 648 word replacement
- 三问TDM
猜你喜欢
2021ICPC上海 H.Life is a Game Kruskal重构树
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
ROS2专题(03):ROS1和ROS2的区别【02】
PCI-Express接口的PCB布线规则
Three questions TDM
JS get the key and value of the object
经纬度PLT文件格式说明
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
Explain
LDO voltage stabilizing chip - internal block diagram and selection parameters
随机推荐
2022 届的应届生都找到工作了吗?做自媒体可以吗?
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
Unity3D学习笔记5——创建子Mesh
HDU 4747 Mex「建议收藏」
Two kinds of curves in embedded audio development
STL标准模板库(Standard Template Library)一周学习总结
2022第六季完美童模陕西总决赛圆满落幕
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
Add data analysis tools in Excel
USB (XIV) 2022-04-12
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
USB (XVI) 2022-04-28
系统设计概述
系统架构设计师备考经验分享:论文出题方向
ROS2专题(03):ROS1和ROS2的区别【01】
PHP uses Alibaba cloud storage
MySQL Index Optimization Practice I
B_QuRT_User_Guide(39)
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
How can we make money by making video clips from our media?