[LeetCode][python3]0006. ZigZag Conversion

  1. My first try
class Solution:
def convert(self, s: str, numRows: int) -> str:
gap=(numRows-1)*2
if len(s)<2 or gap<=0:
return s

ans=[]
for i in range(numRows):
if i==0 or i==numRows-1:
ans+=s[i::gap]
#print(ans)
else:

dic={}
index=0
for item in s[i::gap]:
dic[index]=item
index+=2
index=1
for item in s[(gap-i)::gap]:
dic[index]=item
index+=2
for index in range(len(dic)):
ans.append(dic[index])
#print(ans)
return ''.join(ans)
N2I -2020.03.15
class Solution:
def convert(self, s: str, numRows: int) -> str:
#n strings and add them together
listy = [''] * numRows
sign, counter = 1, 0
if numRows == 1:
return s
else:
for i in s:
listy[counter] += i
counter = counter + sign
if counter == numRows - 1:
sign = -1
if counter == 0:
sign = 1

finalStr = ''.join(listy)
return finalStr
N2I -2020.03.15

--

--

--

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Security for Drupal and related server software

Difference between loc() and iloc() in Pandas DataFrame

The Skill That Makes a Developer Stand Out

Work meeting

Practical Azure: Secure a .NET Core Web API using Azure AD B2C.

Deploying a Flask App on Heroku and Connecting it to MongoDB’s mLab

CS373 Fall 2020: Ivan Romero

Why does the DB function not return the proper power ratio in Signal Processing Toolbox?

Preparing Productive Ramadan App

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
N2I

N2I

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

More from Medium

LeetCode Patterns Adventure 16 — Merge Two Sorted Lists

Two Sum | Interview Question

Introduction to Data Structures and Algorithms with Python: A hands-on Approach

Course Schedule: Leetcode — Blind 75 (Graph)