mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day 2 and 3
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
We can use 2D array instead of vector of vectors but prefer vector<vector<char>> when
|
||||
It has to grow dynamically
|
||||
Different rows have different lengths
|
||||
To save memory
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
class Solution{
|
||||
public:
|
||||
string convert(string s,int numrows){
|
||||
if(numrows == 0|| numsrows>=s.length())
|
||||
return s;
|
||||
vector<vector<char>> rows(numrows);
|
||||
int index = 0, direc = 1;
|
||||
|
||||
for(char c: s){
|
||||
rows[index].push_back(c);
|
||||
if(index == 0)
|
||||
direc =1;
|
||||
else if(index == numrows -1)
|
||||
direc = -1;
|
||||
index+=direc;
|
||||
}
|
||||
string res;
|
||||
for(const auto &row : rows)
|
||||
for(char c : row)
|
||||
res+=c;
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user