当前位置:网站首页>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
边栏推荐
- Unity3D学习笔记4——创建Mesh高级接口
- php 使用阿里云存储
- 经纬度PLT文件格式说明
- Mysql索引优化实战二
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- Coreseek:第二步建索引及測试
- Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
- USB (XV) 2022-04-14
- 七月第一周
- USB (XVI) 2022-04-28
猜你喜欢

Unity3D学习笔记6——GPU实例化(1)

The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way

2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?

UE4_UE5结合罗技手柄(F710)使用记录

七月第一周
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

First week of July

Spark 离线开发框架设计与实现

LDO穩壓芯片-內部框圖及選型參數
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)
Ros2 topic (03): the difference between ros1 and ros2 [01]
随机推荐
MySQL Index Optimization Practice I
Matlab SEIR infectious disease model prediction
First week of July
System design overview
MySQL Index Optimization Practice II
Inftnews | the wide application of NFT technology and its existing problems
648. Word replacement
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
Happy gathering time
三问TDM
LDO稳压芯片-内部框图及选型参数
Matlab-SEIR传染病模型预测
v-for遍历对象
UE4_ Ue5 combined with Logitech handle (F710) use record
turbo intruder常用脚本
V-for traversal object
B_QuRT_User_Guide(37)
Archlinux install MySQL
做自媒体视频剪辑怎么赚钱呢?
Tree background data storage (using webmethod) [easy to understand]