88. Merge Sorted Array【easy】
88. Merge Sorted Array【easy】
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.
解法一:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - ;
int j = n - ;
int len = m + n - ;
if (n == )
{
return;
}
while (i >= && j >=)
{
if (nums1[i] >= nums2[j])
{
nums1[len--] = nums1[i--];
}
else
{
nums1[len--] = nums2[j--];
}
}
/*
while (i >= 0)
{
nums1[len--] = nums1[i--];
}
*/
while (j >= )
{
nums1[len--] = nums2[j--];
}
}
};
从后向前搞,nums2完毕之后就不用处理nums1了,因为都是往nums1中合并的
解法二:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - , j = n - , tar = m + n - ;
while (j >= ) {
nums1[tar--] = i >= && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
}
}
};
This code relies on the simple observation that once all of the numbers from nums2 have been merged into nums1, the rest of the numbers in nums1 that were not moved are already in the correct place.
88. Merge Sorted Array【easy】的更多相关文章
- 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88 Merge Sorted Array(归并排序Easy)
题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中 class Solution { public: void merge(vector<int>& nums ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- Spoj Query on a tree III
题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...
- 【可持久化Trie】【set】bzoj3166 [Heoi2013]Alo
枚举每个数,计算以其为次大数的最大区间,显然,只需要用这个区间的答案 对 答案进行更新即可. 找到每个数右侧.左侧第1.2个比它大的数,然后分类讨论一下即可. 找到的过程中把数sort以后,从大到小把 ...
- Java高级架构师(一)第12节:Service的实现以及模块化
BaseService.java package com.sishuok.architecture1.common.service; import java.util.List; import com ...
- Jquery中的ajax应用(第九章PPT)
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1&q ...
- VS2017安装错误:工作负荷不完整,未能安装包“sqlcmdlnutils,version=15.1.61703.130,chip=x64,language=zh-CN”。
场景:已安装的VS2017维护安装MVC4时出现如下错误: 看问题描述是由于sqlcmdlnutils安装失败影响到其它组件的安装,于是单独下载此安装包进行安装,发现安装一切正常,继续维护VS2017 ...
- redis基本命令,配置参数
https://www.w3cschool.cn/redis/redis-pub-sub.html redis-server --maxclients 100000 客户端命令CLIENT LIST ...
- 多重采样(MultiSample)下的FBO反锯齿 【转】
在三维渲染的过程中,锯齿总是让人讨厌的东西.抗锯齿的一种采用方式是多重采样,本文主要小记一下FBO与多重采样的关系.——ZwqXin.com 首先,关于FBO(Frame Buffer Object) ...
- Python 获取图片格式及像素宽高信息
# coding: utf8 from PIL import Image img = Image.open("img.jpg") print img.sizeprint img.f ...
- Python 自用代码(知网会议论文网页源代码清洗)
#coding=utf-8 from pymongo import MongoClient from lxml import etree import requests jigou = u" ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC2
右击NC- Configuration,然后Append Task,然后右击Axis,Append Axis 轴的类型可以分为:Continuous Axis,默认的类型,NC可以连续闭环控制该轴 ...