Merge Sorted Array 【leetcode】
2015-07-09 09:47
330 查看
Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal tom +
n) to hold additional elements from nums2. The number of elements initialized innums1 and
nums2 are m and n respectively.
读题才是最重要的,我一开始并没有看懂需要排序
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index2=0;
int index1=m;
while(index2!=n){
nums1[index1]=nums2[index2];
index1++;
index2++;
}
for(int i = 0 ; i < nums1.length ; i ++) {
for(int j = i +1 ; j < nums1.length ; j ++) {
if(nums1[i] > nums1[j]) {
int temp = nums1[i];
nums1[i] = nums1[j];
nums1[j] = temp;
}
}
}
}
}
Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal tom +
n) to hold additional elements from nums2. The number of elements initialized innums1 and
nums2 are m and n respectively.
读题才是最重要的,我一开始并没有看懂需要排序
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index2=0;
int index1=m;
while(index2!=n){
nums1[index1]=nums2[index2];
index1++;
index2++;
}
for(int i = 0 ; i < nums1.length ; i ++) {
for(int j = i +1 ; j < nums1.length ; j ++) {
if(nums1[i] > nums1[j]) {
int temp = nums1[i];
nums1[i] = nums1[j];
nums1[j] = temp;
}
}
}
}
}
相关文章推荐
- cout运算顺序(←)
- 机器学习——Dropout原理介绍
- [整理]邮件里常用英文缩写
- Python实现文件按照日期命名的方法
- Asterisk 拨号计划之匹配规则和优先级详解
- HTTP请求及应答
- iOS8 UIApplicationOpenSettingsURLString
- 访问单个节点的删除
- 杭电acm 1236 排名 list排序
- Haskell心得(1) 模式匹配、高阶函数、组合函数
- Linux智能小开关rfkill
- 对话框程序SW_HIDE 和 SW_MINIMIZE 区别
- 使用jquery-mockjax模拟ajax请求做前台测试
- 【CODE】js转换时间格式:将时间转换成自己想要的格式
- 列表页模板 常识
- R语言的帮助使用和图形功能简介
- 怎样知道linux是否安装了mysql
- 新建组
- Android手势操作
- UIWebView用法详解及代码分享