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 = ...
随机推荐
- AtCoder - 2705 Yes or No
Problem Statement You are participating in a quiz with N+M questions and Yes/No answers. It's known ...
- 【第二类Stirling数】Gym - 101147G - The Galactic Olympics
如果K>n,就无解: 如果K==n,就答案是P(n,n): 如果K<n,答案就是s(n,K)*P(K,K): P为排列数,s为第二类斯特林数. 第二类斯特林数就是将n个球,划分为K个非空集 ...
- 1.6(学习笔记)EL表达式
1.表达式输出属性 先来看一个简单的表达式小例子 el.jsp <%@ page language="java" contentType="text/html; c ...
- cocos2d 文件系统使用文件内存映射性能对比
//cocos 修改代码 ..... //性能测试代码 extern "C" { #include <time.h> #include <stdlib.h> ...
- Problem A: 调用函数,求三个数中最大数
#include<stdio.h> int max(int a,int b,int c); int main() { int a,b,c; while(scanf("%d %d ...
- mongodb_profier
http://docs.mongodb.org/manual/reference/database-profiler/ 一.获取.设置profile(profile用collection存储数据) d ...
- VUE -- Vue.js每天必学之计算属性computed与$watch
在模板中绑定表达式是非常便利的,但是它们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护.这就是为什么 Vue.js 将绑定表达式限制为一个表达式.如果需 ...
- AtomicInteger类的简单应用
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...
- 一起來玩鳥 Starling Framework(8)BitmapFont
所謂BitmapFont,就是事先將我們會用到的字型,會用到的字,輸出成一張圖片,類似Sprite sheet,以及一個xml格式的Data file,然後我們一次將這文字圖片轉成Texture,up ...
- 一起來玩鳥 Starling Framework 簡介
開場 Starling Framework是一套Flash 2D遊戲開發"工具",是使用Flash最新的Stage3D API建構出來的一套Framework.最大優點在於使用GP ...