LintCode之合并排序数组
题目描述:

我的代码:
public class Solution {
/*
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
// write your code here
int[] a = new int[A.length+B.length];
int p=0,q=0,count=0;
//当有一个数组全都重新排好序之后就退出循环
while(p<A.length && q<B.length) {
if(A[p] <= B[q]) {
a[count++] = A[p];
p++;
}else {
a[count++] = B[q];
q++;
}
}
if(p < A.length) {
for(int i=p; i<A.length; i++) {
a[count++] = A[i];
}
}
if(q < B.length) {
for(int i=q; i<B.length; i++) {
a[count++] = B[i];
}
}
return a;
}
}
LintCode之合并排序数组的更多相关文章
- 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 { /* * @ ...
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- lintcode: 寻找旋转排序数组中的最小值
寻找旋转排序数组中的最小值 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的 ...
- lintcode:寻找旋转排序数组中的最小值 II
寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在重复的元 ...
- lintcode.177 把排序数组转换为高度最小的二叉搜索树
把排序数组转换为高度最小的二叉搜索树 描述 笔记 数据 评测 给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 注意事项 There may exist multiple val ...
- 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之后,然后 ...
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- [LintCode] 合并排序数组II
class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...
随机推荐
- hbase报Dead Region Servers
问题描述: 16010端口启动成功,16020未启动. hbase-root-regionserver-hbase2.log日志: 2019-08-14 16:45:10,552 WARN [Thre ...
- vue封装分页组件
element提供的分页是已经封装好的组件,在这里再次封装是为了避免每个用到分页的页面点击跳转时都要写一遍跳转请求 分页组件 <!--分页组件--> <template> &l ...
- Markov Decision Process in Detail
From the last post about MDP, we know the environment consists of 5 basic elements: S:State Space of ...
- Pair Testing
All-Pairs Testing is a test design method to deal with the combinatorics problem of defining test ca ...
- 高级ACL访问控制列表
实验拓扑: 配置: 基本配置做完之后搭建OSPF网络 R1: ospf 1 area 0 network 10.0.13.0 0.0.0.255 network 1.1.1.1 0.0.0.0 R2: ...
- jsp(java server page)
jsp的组成元素; 1, 指令 page指令 <%@ page ..........%> language---当前页面使用的语言:java import---当前页面引入的类库, 默认是 ...
- shell 删除项目日志
删除半年之前的日志 find 后面紧跟目录 .为当前目录 -type f 指定查找的是文件 -mtime +180 查找180天之前的 -name 文件名筛选 -exec -rm -rf 执行的 ...
- df认识
import pandas as pd #自己创建一个df df = pd.DataFrame({ ,,], 'col2':["zs",'li','zl'], 'col3':[3. ...
- 这样设计 Java 异常更优雅,赶紧学!
来源:lrwinx.github.io/2016/04/28/如何优雅的设计java异常/ 导语 异常处理是程序开发中必不可少操作之一,但如何正确优雅的对异常进行处理确是一门学问,笔者根据自己的开发经 ...
- NGUI的CheckBox的使用(toggle script)
一,我们先添加一个sprite,选择sprite,右键选择attach,添加box collider, 然后右键选择attach,添加toggle script,得到如下图结果 1,但是如果你没有给U ...