LeetCode之Merge Sorted Array

举报
chenyu 发表于 2021/07/26 23:35:39 2021/07/26
【摘要】 1、问题 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enou...

1、问题

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.

 

2、代码实现


   
  1. package leetcode.chenyu.test;
  2. public class MergeSortedArray {
  3. public static void main(String[] args) {
  4. int a[] = new int[10];
  5. a[0] = -1;
  6. a[1] = 0;
  7. a[2] = 4;
  8. a[3] = 7;
  9. int b[] = {2, 5};
  10. int len = a.length;
  11. for (int x : a)
  12. System.out.print(x);
  13. merge(a, 10, b, 2);
  14. for (int x : a)
  15. System.out.print(x);
  16. }
  17. public static void merge(int[] nums1, int m, int[] nums2, int n) {
  18. if (nums1 == null || m == 0 || n == 0)
  19. return;
  20. int i = 0, j = 0, k = 0;
  21. int index = 0;
  22. int nums3[] = new int[m];
  23. for (int x = m - 1; x>= 0; x--) {
  24. if (x - 1 > 0)
  25. if (nums1[x] == 0 && nums1[x - 1] != 0) {
  26. index = x;
  27. break;
  28. }
  29. }
  30. int value = nums1[index];
  31. System.out.println("index is:" + index);
  32. while (j < n && i < index) {
  33. if (nums1[i] <= nums2[j]) {
  34. System.out.print("if i is " + i + "k is " + k + "nums1[" + i + "]" + nums1[i] + "\n");
  35. nums3[k] = nums1[i];
  36. i++;
  37. } else {
  38. System.out.print("if j is " + j + "k is " + k + "nums2["+ j + "]" + nums2[j] + "\n");
  39. nums3[k] = nums2[j];
  40. j++;
  41. }
  42. k++;
  43. }
  44. System.out.println("i + 1 < m" + (i + 1 < m));
  45. System.out.println("i is :" + i);
  46. System.out.println("nums1[i]" + nums1[i]);
  47. System.out.println("nums1[index]" + nums1[index]);
  48. System.out.println("nums1[i + 1] != nums1[index]" + (nums1[i + 1] != nums1[index]));
  49. if (i + 1 < m && nums1[i] != nums1[index]) {
  50. System.out.println("if if");
  51. for (int f = i; f < index; f++) {
  52. nums3[k] = nums1[f];
  53. }
  54. }
  55. for (int x : nums3) {
  56. System.out.print(x);
  57. }
  58. for (int h = 0; h < nums3.length; h++) {
  59. nums1[h] = nums3[h];
  60. }
  61. // nums1 = nums3;
  62. }
  63. }

 
 

3、结果


   
  1. -1047000000index is:4
  2. if i is 0k is 0nums1[0]-1
  3. if i is 1k is 1nums1[1]0
  4. if j is 0k is 2nums2[0]2
  5. if i is 2k is 3nums1[2]4
  6. if j is 1k is 4nums2[1]5
  7. i + 1 < mtrue
  8. i is :3
  9. nums1[i]7
  10. nums1[index]0
  11. nums1[i + 1] != nums1[index]false
  12. if if
  13. -1024570000-1024570000

 
提到上去会有下标越界异常,好吧,后面再来分析为什么出错,今天先记录到这里

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/77626163

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。