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

力扣练习1

1.数组串联问题

就将一个n长度的数组变成2n,并将里面的值再复制一份放进去。

Java:

class Solution { public int[] getConcatenation(int[] nums) { //创建新数组 int l=nums.length; int[]ans=new int [2*l]; for(int i=0;i<l;i++){ ans[i]=nums[i]; ans[i+l]=nums[i]; } return ans; } }

C

int* getConcatenation(int* nums, int numsSize, int* returnSize) { int n=numsSize; *returnSize=2*n; int *ans=(int*)malloc(sizeof(int)*2*n); for(int i=0;i<n;i++){ ans[i]=nums[i]; ans[i+n]=nums[i]; } return ans; }

C++

class Solution { public: vector<int> getConcatenation(vector<int>& nums) { vector<int> ans = nums; ans.insert(ans.end(), nums.begin(), nums.end()); return ans;} };
#include <vector> using namespace std; class Solution { public: vector<int> getConcatenation(vector<int>& nums) { int n = nums.size(); vector<int> ans(2 * n); for (int i = 0; i < n; i++) { ans[i] = nums[i]; ans[i + n] = nums[i]; } return ans; } };

python

class Solution(object): def getConcatenation(self, nums): return nums+nums def getConcatenation(nums): n = len(nums) ans = [0] * (2 * n) for i in range(n): ans[i] = nums[i] ans[i + n] = nums[i] return ans

python3

class Solution: def getConcatenation(self, nums: list[int]) -> list[int]: return nums + nums class Solution: def getConcatenation(self, nums: list[int]) -> list[int]: n = len(nums) # 创建长度 2n 的数组 ans = [0] * (2 * n) for i in range(n): ans[i] = nums[i] ans[i + n] = nums[i] return ans

2.重新排列数组

Java

class Solution { public int[] shuffle(int[] nums, int n) { int arr[]=new int[2*n]; int j=0; for(int i=0;i<n;i++){ arr[j++]=nums[i]; arr[j++]=nums[i+n]; } return arr; } }

Python3

class Solution: def shuffle(self, nums: list[int], n: int) -> list[int]: res = [] # 新建空数组 for i in range(n): res.append(nums[i]) # 加入 x1, x2... res.append(nums[i + n]) # 加入 y1, y2... return res

C

int* shuffle(int* nums, int numsSize, int n, int* returnSize) { *returnSize = 2 * n; // 返回数组长度 int* res = (int*)malloc(2 * n * sizeof(int)); // 手动申请内存 for (int i = 0; i < n; i++) { res[2 * i] = nums[i]; // 偶数位放 x res[2 * i + 1] = nums[i + n]; // 奇数位放 y } return res; }

C++

class Solution { public: vector<int> shuffle(vector<int>& nums, int n) { vector<int> res; for (int i = 0; i < n; i++) { res.push_back(nums[i]); res.push_back(nums[i + n]); } return res; } };

C#

public class Solution { public int[] Shuffle(int[] nums, int n) { int[] res = new int[2 * n]; for (int i = 0; i < n; i++) { res[2 * i] = nums[i]; res[2 * i + 1] = nums[i + n]; } return res; } }
http://www.jsqmd.com/news/740291/

相关文章:

  • 如何3秒破解百度网盘密码?终极智能提取码获取工具揭秘
  • 折腾笔记[56]-使用kimi批量进行英文文献翻译
  • 8大网盘直链下载神器:告别限速,一键获取真实下载地址
  • Seraphine:英雄联盟玩家的终极智能助手,全面提升你的游戏体验
  • 广州电子式动态平衡电动调节阀哪家好
  • 别再被Cartopy的‘白线’坑了!一个add_cyclic_point函数搞定全球数据可视化
  • 折腾笔记[53]-使用kimi转换latex到pdf
  • 如何快速掌握抖音下载器:面向新手的完整批量下载指南
  • 别再死记50欧姆了!从PCB走线到同轴线,一文搞懂特征阻抗的底层逻辑
  • 别再死记硬背了!用Python和PyTorch亲手画一遍Sigmoid、Tanh、ReLU激活函数,理解立马不一样
  • 折腾笔记[55]-使用kimi转换markdown为pdf
  • CF1608F MEX counting
  • Virtuoso ADE XL参数扫描实战:用gmid曲线指导MOS管尺寸优化(以IC618为例)
  • OTA校验失败、CRC对不上、版本号错乱——C语言固件升级链路11个关键断点调试技巧,工程师私藏手册
  • 折腾笔记[52]-使用kimi发送消息到matrix房间
  • 为内容创作平台集成 Taotoken 提供多样化的文本生成风格
  • 为什么你的Horovod训练总OOM?20年HPC架构师首次公开:4层内存泄漏配置链路与实时诊断脚本
  • MultiTimer vs. FreeRTOS软件定时器:在资源受限的STM32F4上,我为什么选择了它?
  • WorkshopDL:无需Steam客户端,轻松下载Steam创意工坊模组的终极方案
  • 别再死磕YOLOv5了!用CLIP+CRIS结构,手把手教你实现文本驱动的目标检测
  • 2026届学术党必备的十大AI辅助论文方案横评
  • 20260430
  • DataChain:构建面向对象存储的数据上下文层,实现AI时代数据处理革命
  • Stata数据合并保姆级避坑指南:从CSV导入到merge命令的完整流程
  • Windows 11 24H2 LTSC 微软商店一键安装完整指南:如何3分钟恢复完整应用生态
  • 杭州萧山区在职提升学历哪家好?萧山箭金学堂等五大机构深度测评榜 - 浙江行业评测
  • 3分钟搞定Android Studio中文界面:新手必备的完整免费汉化指南
  • 别再到处找了!电气AI项目数据集保姆级导航(含无人机巡检、负荷预测等60+资源)
  • 模型部署前必看:用Netron快速检查ONNX、TensorFlow模型结构,避开这些坑
  • FPGA新手避坑指南:用Verilog写自己的‘软’ROM存储波形,真的比用IP核好吗?