当前位置:网站首页>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
边栏推荐
- 经纬度PLT文件格式说明
- ROS2专题(03):ROS1和ROS2的区别【02】
- Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
- js 获取对象的key和value
- UE4_UE5全景相机
- 包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
- USB (XVII) 2022-04-15
- MATLAB signal processing [Q & A essays · 2]
- Dynamics 365 find field filtering
- Three questions TDM
猜你喜欢
随机推荐
在软件工程领域,搞科研的这十年!
B_QuRT_User_Guide(36)
Deep understanding of MySQL lock and transaction isolation level
B_QuRT_User_Guide(40)
Oracle-数据库的备份与恢复
V-for traversal object
CXF call reports an error. Could not find conduct initiator for address:
Three questions TDM
USB(十四)2022-04-12
Inftnews | the wide application of NFT technology and its existing problems
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
First week of July
Bea-3xxxxx error code
Solution: prompt "unsupported video format" when inserting avi format video into the message
Inftnews | web5 vs Web3: the future is a process, not a destination
js 获取对象的key和value
CAIP2021 初赛VP
UE4_ Ue5 combined with Logitech handle (F710) use record
Unity3D学习笔记4——创建Mesh高级接口
UE4_UE5结合罗技手柄(F710)使用记录








