[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

Uptick adds Japanese language support

N00b to Pro to N00b to Pro, Ad Nauseam: The Process of Learning to Code

Build an HTML Email Template From Scratch

ScandiPWA Updates: Issue 45

The Open Banking Interviews: Koen Adolfs, Product Owner & API Evangelist, ABN AMRO Bank

Breakout Sessions continued…

How to set-up your Mac Python environment

Skipping Intro Cinematic

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 Blind 75 practice — Contains duplicate

Normalization in Database

LeetCode #167 | Two Sum II — Input Array Is Sorted (Python)

ARRAYS AND STRINGS DATA STRUCTURES.