1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // If you suddenly need values from a 2D
- // array diagonally and clearing it,
- // this function is for You
- //
- // The Ripper of 2d vectors
- //
- // r2dv.cpp
- //
- // Created by Volkov Roman on 19.03.2023.
- //
- #include <stdio.h>
- #include <vector>
- using namespace std;
- vector<int> findDiagonalOrder(vector<vector<int>>& nums) {
- vector<int> result;
- int i=0, j=0, x=0, y=0;
- x = nums.size();
- for (i=0; i<x; ++i) {
- if (nums[i].size() > y) {
- y = nums[i].size();
- }
- }
- const int iters_summ = x + y - 1;
- int max=0;
- for (i=0; i < iters_summ; i++){
- j = i;
- if (i > x-1){
- max = x;
- j = x-1;
- } else {
- max++;
- }
- int j0;
-
- for (j0=max; j0>0; j0--){
- if (nums[j].empty()){
- j--;
- continue;
- }
- result.push_back(nums[j][0]);
- nums[j].erase(nums[j].begin());
- j--;
- }
- }
- return result;
- }
- int main() {
- /*
- vector < vector <int> > ivector = {{1,3,6,8},
- {2,5},
- {4,7}};
- vector<int> test = findDiagonalOrder(ivector);
- for (int i=0; i < test.size(); i++) {
- //12435768
- cout << test[i] << "";
- }
- cout << endl;
- cout << "Ivector size: " << ivector.size() << endl; //3
- for (int i=0; i < ivector.size(); i++) {
- //124357689
- cout << "Ivector[" <<i << "] Size: " << ivector[i].size() << endl;
- // Ivector[0] Size: 0
- // Ivector[1] Size: 0
- // Ivector[2] Size: 0
- }
- */
- return 0;
- }
|