标签:
1 class Solution { 2 public: 3 void reOrderArray(vector<int> &array) { 4 if (array.size() == 0) 5 return; 6 int i = 0, j = array.size() - 1; 7 vector<int> v1, v2; 8 9 for (int i = 0; i < array.size(); i++){ 10 if (array[i] % 2 != 0) 11 v1.push_back(array[i]); 12 else 13 v2.push_back(array[i]); 14 } 15 int k = 0; 16 for (int j = 0; j < v1.size(); j++){ 17 array[k] = v1[j]; 18 k++; 19 } 20 for (int j = 0; j < v2.size(); j++){ 21 array[k] = v2[j]; 22 k++; 23 } 24 } 25 };
1 class Solution { 2 public: 3 void reOrderArray(vector<int> &array) { 4 if (array.size() == 0) 5 return; 6 int i = 0, j = array.size() - 1; 7 while (j > i){ 8 if (array[i] % 2 != 0) 9 i++; 10 if (array[j] % 2 == 0) 11 j--; 12 if (array[i] % 2 == 0 && array[j] % 2 != 0){ 13 int temp = array[i]; 14 array[i] = array[j]; 15 array[j] = temp; 16 } 17 } 18 } 19 };
标签:
原文地址:http://www.cnblogs.com/wanderingzj/p/5352509.html