64. 合并排序数组.md
描述
合并两个排序的整数数组A和B变成一个新的数组。
你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。
您在真实的面试中是否遇到过这个题?
样例
给出 A = [1, 2, 3, empty, empty]
, B = [4, 5]
合并之后 A 将变成 [1,2,3,4,5]
public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int totalLength = m+n;
while (n != 0 && m != 0) {
int curA = A[m - 1];
int curB = B[n - 1];
if (curA > curB) {
A[totalLength - 1] = curA;
m --;
} else {
A[totalLength - 1] = curB;
n --;
}
totalLength --;
}
//如果数组A的长度为0;前面的直接不执行
while (n != 0) {
A[totalLength - 1] = B[n - 1];
n--;
totalLength--;
}
// while (aSize != 0) {
// }
}
}
public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
if(n == 0){
return;
}
if(m == 0){
for(int l = 0 ; l < n ; l++){
A[l] = B[l];
}
return;
}
int[] C = new int[m + n];
int i = 0;
int j = 0;
int num = 0;
boolean flag = false;
for(; i < m ; ){
// write your code here
for(; j < n ; ){
if(A[i] < B[j]){
C[num] = A[i];
num++;
i++;
if(i == m){
flag = true;
break;
}
}else{
C[num] = B[j];
j++;
num++;
if(j == n){
flag = true;
break;
}
}
}
if(flag){
break;
}
}
if(i != m){
for(; i < m ; i++){
C[num] = A[i];
num++;
}
}
if(j != n){
for(; j < n ; j++){
C[num] = B[j];
num++;
}
}
if(num == m + n){
for(int l = 0 ; l < m+n ; l++){
A[l] = C[l];
}
return;
}
}
}
64. 合并排序数组.md的更多相关文章
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- lintcode:合并排序数组
题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- 6. 合并排序数组 II
6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...
- LintCode——合并排序数组II
描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...
- 怎样合并排序数组(How to merge 2 sorted arrays?)
Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...
- leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- LintCode之合并排序数组
题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...
随机推荐
- Win#password;;processon #clone;;disassemble;;source find
1.密码学思维导图 源地址:https://www.processon.com/view/5a61d825e4b0c090524f5b8b 在这之前给大家分享 如何在 processon上搜索公开克隆 ...
- WireShark Flow capture analysis
Wiresharkl流量分析 1.图示是对WiFi数据接口的80号端口进行数据捕获,设置混杂模式 过滤表达式设置: IP地址设置过滤 ip.src==191.168.1.102 ip.dst ...
- truncate table时存在外键约束的解决办法
以前在使用truncate命令时遇到表存在外键引用时无法执行命令的情况都是用delete来代替,今天又遇到这个问题,于是在网上搜了一把,可以通过如下方式解决: 1.基本思路:先关闭mysql的外键约束 ...
- Python函数之递归函数
递归函数的定义:在这个函数里再调用这个函数本身 最大递归深度默认是997或者998,python从内存角度做的限制 优点:代码变简单 缺点:占内存 一:推导年龄 问a的值是多少: a 比 b 小2,b ...
- html表单的使用
表单用于搜集不同类型的用户输入,表单由不同类型的标签组成,实现一个特定功能的表单区域(比如:注册),首先应该用<form>标签来定义表单区域整体,在此标签中再使用不同的表单控件来实现不同类 ...
- Python模拟人猜数过程(折半查找)
import random# (0,1000)随机产生一个数key = random.randint(1,1000)# 用来统计猜的次数count = 0 # 定义一个折半查找的函数def BinSe ...
- 访问和获取Cookie
Cookie[] cookies = request.getCookies();//从会话里取出Cookie并放在Cookie数组里 if(cookies!=null) { for(Cookie c: ...
- .net C# 抽奖,中奖
demo设置了8个奖项,每个奖项可以自定义中奖率,精度为1/10000 public string PrizeDraw() { //奖品以及中奖率 const string prizeString = ...
- python property的用法
用法一: class Test(object): def __init__(self): # 私有化 self.__num = 100 #名字重整_Test__num def setNum(self, ...
- PV-UV-QPS
QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...