博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
88. Merge Sorted Array
阅读量:4344 次
发布时间:2019-06-07

本文共 1202 字,大约阅读时间需要 4 分钟。

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

 

 

合并两个有序数组到nums1里,不需要去重

 

C++(3ms):

1 class Solution { 2 public: 3     void merge(vector
& nums1, int m, vector
& nums2, int n) { 4 int i = m-1 ; 5 int j = n-1 ; 6 int k = m+n-1 ; 7 while(j >= 0){ 8 if (i >=0 && nums1[i] > nums2[j] ){ 9 nums1[k--] = nums1[i--] ;10 }else{11 nums1[k--] = nums2[j--] ;12 }13 }14 }15 };

 

java(0ms):

1 class Solution { 2     public void merge(int[] nums1, int m, int[] nums2, int n) { 3         int i = m - 1 ; 4         int j = n - 1 ; 5         int k = m + n - 1 ; 6         while(j >= 0){ 7             if (i >= 0 && nums1[i] > nums2[j]) 8                 nums1[k--] = nums1[i--] ; 9             else10                 nums1[k--] = nums2[j--] ;11         }12     }13 }

 

转载于:https://www.cnblogs.com/mengchunchen/p/7566759.html

你可能感兴趣的文章
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>