Leetcode day 2 and 3

This commit is contained in:
2026-01-29 01:04:37 +05:30
parent 245116d181
commit 2ee49ea476
15 changed files with 204 additions and 1 deletions
+5
View File
@@ -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
+24
View File
@@ -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;
}
};