Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 837ebd9

Browse files
Create 06. Zigzag Conversion
1 parent 24efc83 commit 837ebd9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎06. Zigzag Conversion

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
string convert(string s, int nums) {
4+
int n = s.length();
5+
if(nums <= 1 || nums> n)
6+
return s;
7+
8+
vector<string> str(nums);
9+
int pos = -1; // can be -1 or 1
10+
int row = 0;
11+
for(auto c: s)
12+
{
13+
string st;
14+
str[row].push_back(c);
15+
st+= c;
16+
if(row == 0 || row == nums-1) // for checking our position in the zig zag whether top or bottom
17+
{
18+
pos*=-1; // for the zig zag pattern
19+
}
20+
row+=pos; // if position is decremented this means that we're accessing the elements in the diagonal
21+
22+
}
23+
string temp = "";
24+
for(auto c: str)
25+
for(auto ch: c)
26+
temp+=ch;
27+
28+
return temp;
29+
}
30+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /