The size of the array is 18 1 2 3 7 5 1 1 3 7 4 7 3 2 8 1 1 7 6
The corresponding codes:
#includeThe second is to sort out the elements in array in increasing order:#include #include #include #include #include #include using namespace std; void generate_array(vector & arr, int n); int main() { cout << "The size of the array is " ; int n; cin >> n; srand((unsigned)time(NULL)); vector arr; generate_array(arr, n); for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;}); return 0; } void generate_array(vector & arr, int n) { for (int i = 0; i < n; i++){ arr.push_back(rand() % 10); } }
the results:
The size of the array is 16 4 1 3 8 9 6 4 8 6 4 1 5 9 8 8 5 The show of the elements in the array: 1 1 3 4 4 4 5 5 6 6 8 8 8 8 9 9
The corresponding codes:
#include#include #include #include #include #include #include using namespace std; void generate_array(vector & arr, int n); int main() { cout << "The size of the array is " ; int n; cin >> n; srand((unsigned)time(NULL)); vector arr; generate_array(arr, n); for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;}); cout << endl; sort(arr.begin(), arr.end()); cout << "The show of the elements in the array:" << endl; for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;}); return 0; } void generate_array(vector & arr, int n) { for (int i = 0; i < n; i++){ arr.push_back(rand() % 10); } }
The third steps to delete all the same elemets in array:
the final version of my results from my codes:
The size of the array is 14 3 9 7 9 4 3 6 6 5 6 3 9 6 3 The show of the elements in the array: 3 3 3 3 4 5 6 6 6 6 7 9 9 9 The size of deleted array: 6 The elements of the array after the deletion: 3 4 5 6 7 9
The corresponding codes:
#include#include #include #include #include #include #include using namespace std; void generate_array(vector & arr, int n); int removeDuplicates(vector & arr); int main() { cout << "The size of the array is " ; int n; cin >> n; srand((unsigned)time(NULL)); vector arr; generate_array(arr, n); for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;}); cout << endl; sort(arr.begin(), arr.end()); cout << "The show of the elements in the array:" << endl; for_each(arr.begin(), arr.end(), [](int x){ cout << setw(2) << x ;}); int end_pos = removeDuplicates(arr); cout << "nThe size of deleted array: " << end_pos << endl; cout << "The elements of the array after the deletion:" << endl; for (int i = 0; i < end_pos; i++){ cout << setw(2) << arr[i] ; } return 0; } void generate_array(vector & arr, int n) { for (int i = 0; i < n; i++){ arr.push_back(rand() % 10); } } int removeDuplicates(vector & arr) { int len = arr.size(); if (len == 0) return 0; int i = 0; // index of left pointer log the position of new different elements int j = 1; // index of right pointer log the fisrt position of new different elements for (i, j; j < len; ){ if (arr[i] == arr[j]){ j++; }else{ i++; arr[i] = arr[j]; j++; } } return i + 1; }
https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)