C++ 6ms solution


  • 0
    T
       class Solution {
        public:
            void merge(int A[], int m, int B[], int n) {
        		if (n < 1) return;
        		if (m < 1 ){
        			for(int iB = 0; iB != n; iB++){
        				*(A+iB) = *(B+iB);
        			}
                            return;
        		}
        		int last_index = 0;
        		bool notcmp = false;
        		for(int iB = 0; iB != n; iB++){
        			if(true == notcmp){
        				*(A+last_index+1) = *(B+iB);
        				++last_index;
        			}
        			else{
        				for(int iA = last_index; iA != m + iB; iA++){
        					if(*(B+iB) < *(A+iA)){
        						last_index = iA + 1;
        						for(int move = m + iB; move != iA; --move){
        							*(A+move) = *(A+move-1);
        						}
        						*(A+iA) = *(B+iB);
        						break;
        					}else if(iA+1 == m+iB){
        						*(A+iA+1) = *(B+iB);
        						last_index = iA + 1;
        						notcmp = true;
        					}
        				}
        			}
        		}
        		return;
            }
        
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.