标签:style blog color io ar for sp div on
做这道题的时候思路不是很清晰。写的乱七八糟!!
挺简单的一道题,向别人写的学习!!
package Leetcode; public class MergeSortedArray { public static void merge(int A[], int m, int B[], int n) { int i = m + n - 1; int j = m - 1; int k = n - 1; if (n != 0 && m != 0) { for (; i >= 0 && j >= 0 && k >= 0; i--) { if (A[j] > B[k]) { A[i] = A[j]; j--; } else { A[i] = B[k]; k--; } } if (j < 0) { for (; k >= 0; k--) A[k] = B[k]; } } if (m == 0) { for (; i >= 0; i--) { A[i] = B[k]; k--; } } } }
public class Solution { public void merge(int A[], int m, int B[], int n) { int index = m + n; while (m > 0 && n > 0) { if (A[m - 1] > B[n - 1]) { A[--index] = A[--m]; } else { A[--index] = B[--n]; } } while (n > 0) { A[--index] = B[--n]; } } }
标签:style blog color io ar for sp div on
原文地址:http://www.cnblogs.com/lilyfindjobs/p/4051803.html