【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
解法一:从前往后,小的排最前。每次插入一个B,A需要整体后移。
- class Solution {
- public:
- void merge(int A[], int m, int B[], int n) {
- int indA = ;
- int indB = ;
- int shift = ;
- while(indA < m+shift && indB < n)
- {
- if(A[indA] <= B[indB])
- indA++;
- else
- {
- for(int i = m-+shift; i >= indA; i --)
- A[i+] = A[i];
- shift ++;
- A[indA++] = B[indB++];
- }
- }
- if(indA >= m+shift)
- {
- while(indB < n)
- A[indA++] = B[indB++];
- }
- }
- };
解法二:从后往前,大的排最后。不需要多余的移动。
- class Solution {
- public:
- void merge(int A[], int m, int B[], int n) {
- int indA = m-;
- int indB = n-;
- for(int i = m+n-; i >= ; i --)
- {
- if(indA < )
- {// A finished
- A[i] = B[indB --];
- }
- else if(indB < )
- {// B finished
- A[i] = A[indA --];
- }
- else
- {
- if(A[indA] >= B[indB])
- A[i] = A[indA --];
- else
- A[i] = B[indB --];
- }
- }
- }
- };
【LeetCode】88. Merge Sorted Array (2 solutions)的更多相关文章
- 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...
- 【LeetCode】88 - Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- 【一天一道LeetCode】#88. Merge Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【easy】88. Merge Sorted Array 合并两个有序数组
合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...
- LeetCode OJ 88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- Leetcode No.88 Merge Sorted Array(c++实现)
1. 题目 1.1 英文题目 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【leetcode❤python】 88. Merge Sorted Array
#-*- coding: UTF-8 -*-class Solution(object): def merge(self, nums1, m, nums2, n): "& ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree
Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...
随机推荐
- css3 实现圆角边框的border-radius属性和实现阴影效果的box-shadow属性
首先我要介绍的是border-radius属性,它的作用是实现圆角边框,其中border-radius:20px;表示,一个’体‘四个角都圆滑20px,其值如果为100px那么圆角度则为最高,如果是正 ...
- Ceph rgws客户端验证
修改/etc/ceph/ceph.conf文件,加入rados gw监听的端口 [client.rgw.rgws] rgw_frontends = "civetweb port=80&quo ...
- 《Small Memory Software:Patterns For System With Limited Memory》读书笔记
原文地址:http://blog.csdn.net/jinzhuojun/article/details/13297447 虽然摩尔定律让我们的计算机硬件得以以指数速度升级,但反摩尔定律又不断消减这些 ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- 反射 Reflect Modifier 修饰符工具类
在查看反射相关的Class.Field .Constructor 等类时,看到他们都有这样一个方法:getModifiers():返回此类或接口以整数编码的 Java 语言修饰符.如需要知道返回的值所 ...
- 在asp.net中使用jQuery实现类似QQ网站的图片切割效果
今天要给大家介绍一个asp.net结合jQuery来切割图片的小程序,原理很简单,大致流程是: 加载原图 --> 用矩形框在原图上选取区域并将选取的顶点坐标和矩形尺寸发送至服务器 --> ...
- Android -- 程序启动画面 Splash
很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐. 我的方式是使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一 ...
- [Algorithm] Universal Value Tree Problem
A unival tree (which stands for "universal value") is a tree where all nodes under it have ...
- 浅谈JavaScript框架设计
在这个js框架随处乱跑的时代,你是否考虑过写一个自己的框架?下面的内容也许会有点帮助. 一个框架应该包含哪些内容? 1.语言扩展 大部分现有的框架都提供了这部分内容,语言扩展应当是以ECMAScrip ...
- Hibernate(七)一对一映射
一.创建数据库表 --班级表 create table grade ( gid number primary key, --班级ID gname ), --班级名称 gdesc ) --班级介绍 ); ...