Skip to content

Create 06. Zigzag Conversion #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 06. Zigzag Conversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
string convert(string s, int nums) {
int n = s.length();
if(nums <= 1 || nums> n)
return s;

vector<string> str(nums);
int pos = -1; // can be -1 or 1
int row = 0;
for(auto c: s)
{
string st;
str[row].push_back(c);
st+= c;
if(row == 0 || row == nums-1) // for checking our position in the zig zag whether top or bottom
{
pos*=-1; // for the zig zag pattern
}
row+=pos; // if position is decremented this means that we're accessing the elements in the diagonal

}
string temp = "";
for(auto c: str)
for(auto ch: c)
temp+=ch;

return temp;
}
};