GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc88.java at master · mJackie/leetcode Expand file tree/ Copy pathlc88.javaMore file actions 35 lines (34 loc) · 962 Bytes / Copy pathlc88.javaFile metadata and controls35 lines (34 loc) · 962 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package code; /* * 88. Merge Sorted Array * 题意:归并数组 * 难度:Easy * 分类:Array, Two Pointers * 注意:实际上 nums1.length = m+n , 不会>m+n ,题意没说清楚. 用一个指针指向nums的末端,从后往前走 */ public class lc88 { public static void main(String[] args) { int[] nums1 = {4,5,6,0,0,0}; int[] nums2 = {1,2,3}; int m = 3; int n = 3; merge(nums1,m,nums2,n); for(int i:nums1) System.out.println(i); } public static void merge(int[] nums1, int m, int[] nums2, int n) { int ptr1 = m-1; int ptr2 = n-1; int cur = m+n-1; while(ptr1>=0 && ptr2>=0){ if(nums1[ptr1]>nums2[ptr2]) { nums1[cur--] = nums1[ptr1--]; } else { nums1[cur--] = nums2[ptr2--]; } } while(ptr2>=0) nums1[cur--] = nums2[ptr2--]; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |