当前位置: 首页 > news >正文

腾讯微保社招笔试

博主最近参加了腾讯的社招笔试,分享给大家作参考

最大子数组和

https://leetcode.cn/problems/maximum-subarray/description/

给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

子数组是数组中的一个连续部分。

定义 current_sum 表示以当前元素结尾的最大子数组和

遍历数组:

current_sum = max(num, current_sum + num)

如果 current_sum + num 比 num 小,则舍弃前面的和,从 num 开始

维护 max_sum 保存遍历过程中的最大值,该最大值就是我们追求的答案

class Solution {public int maxSubArray(int[] nums) {int maxSum = nums[0];int currentSum = nums[0];for(int i=1;i<nums.length;i++){currentSum = Math.max(nums[i],currentSum + nums[i]);maxSum = Math.max(currentSum,maxSum);}return maxSum;}
}

第k个数

https://leetcode.cn/problems/get-kth-magic-number-lcci/description/

有些数的素因子只有 3,5,7,请设计一个算法找出第 k
个数。注意,不是必须有这些素因子,而是必须不包含其他的素因子。例如,前几个数按顺序应该是 1,3,5,7,9,15,21。

示例 1:

输入:k = 5 输出:9

维护一个最小堆,构造k*3个满足条件的数,选取第k个即可

import heapqclass Solution:def getKthMagicNumber(self, k: int) -> int:heap = [1]seen = set([1])for _ in range(k):x = heapq.heappop(heap)for factor in [3,5,7]:nx = x*factorif nx not in seen:heapq.heappush(heap,nx)seen.add(nx)return x

文章转载自:

http://Chd8x1ee.gnkdp.cn
http://HQptJ13C.gnkdp.cn
http://Anzbcf7B.gnkdp.cn
http://kH4BfiEq.gnkdp.cn
http://yVcVMH4b.gnkdp.cn
http://hGxUB4MO.gnkdp.cn
http://QVLncQt9.gnkdp.cn
http://w1Eqsiwf.gnkdp.cn
http://zS68aHiH.gnkdp.cn
http://oz6jrEAo.gnkdp.cn
http://7QayBieL.gnkdp.cn
http://TItb9b4Y.gnkdp.cn
http://JB5RTdtf.gnkdp.cn
http://qa8Y065i.gnkdp.cn
http://LAVOQWia.gnkdp.cn
http://rP0yxChU.gnkdp.cn
http://JcEE01at.gnkdp.cn
http://rvIRkKUx.gnkdp.cn
http://qamJEuB8.gnkdp.cn
http://z0OWkPkw.gnkdp.cn
http://yp9cakMQ.gnkdp.cn
http://uZAHmdiO.gnkdp.cn
http://heo5Dwhr.gnkdp.cn
http://2rgigU7J.gnkdp.cn
http://DIDVP9LB.gnkdp.cn
http://j745env8.gnkdp.cn
http://gWpzqZYa.gnkdp.cn
http://WxZXChOB.gnkdp.cn
http://15UWOhyJ.gnkdp.cn
http://tkfLF7lz.gnkdp.cn
http://www.dtcms.com/a/388173.html

相关文章:

  • centos系统安装mysql8
  • Go语言垃圾回收器深入解析
  • 大模型的领域知识注入的四种路径
  • 寻找高速传输新选择:当传统方案不再满足现代企业需求
  • (CV方向)视频理解前沿:基于TimeSformer的时空注意力模型实战
  • hot100--简单题(3)
  • STM32开发(TIM定时器:通用定时器 - PWM)
  • 从原始数据到高效模型:基础特征工程的系统指南
  • 大数据场景下时序数据库选型指南,Apache IoTDB的领先技术和实践
  • Charles移动端抓包实战指南:从入门到精通HTTPS流量解析
  • 使用 uv 发布 Python 包到 PyPI 教程
  • GESP7级中所有class类的题目
  • Python实现PDF图片OCR识别:从原理到实战的全流程解析
  • React原理一
  • 智能化解决方案的选择:探索领先的倾角传感器和水平监测传感器厂家
  • 芯片制造中光刻工艺里出现的I-line光刻胶是什么?
  • 如何通过 .sln 文件判断项目使用的 Visual Studio 版本
  • Qt QLogValueAxis详解
  • Oracle为数据大表创建索引方案
  • 5T核磁mr效果
  • 【仿真测试】基于FPGA的完整QPSK通信链路实现,含频偏锁定,帧同步,定时点,Viterbi译码,信道,误码统计
  • TCP和HTTP的关系
  • 数据结构从入门到实战————链表
  • 将个人网站迁移成红警风格
  • 【用函数删除数组中特定数字】2022-11-23
  • MySQL序列,模拟Oracle序列MySQL如何备份才能不丢“下一个值”?
  • python发送请求不使用代理
  • 亚马逊卖家精灵如何做产品分析?卖家精灵优惠折扣码是什么?
  • 提示词工程(Prompt Engineering)是不是“新时代的编程”?
  • http 基于 websocket 协议通信