GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/006_ZigZag_Conversion.py at master · robotmechanic/leetcode

Expand file tree

/

006_ZigZag_Conversion.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (48 loc) · 1.41 KB
/

006_ZigZag_Conversion.py

Copy path

File metadata and controls

53 lines (48 loc) · 1.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution(object):
# def convert(self, s, numRows):
# """
# :type s: str
# :type numRows: int
# :rtype: str
# """
# ls = len(s)
# if ls <= 1 or numRows == 1:
# return s
# temp_s = []
# for i in range(numRows):
# temp_s.append(['']*(ls / 2))
# inter = numRows - 1
# col, row = 0, 0
# for i, ch in enumerate(s):
# flag = True
# if (i / inter) % 2 == 1:
# # print i
# flag = False
# if flag:
# temp_s[row][col] = ch
# row += 1
# else:
# temp_s[row][col] = ch
# col += 1
# row -= 1
# result = ''
# for i in range(numRows):
# result += ''.join(temp_s[i])
# return result
def convert(self, s, numRows):
# https://leetcode.com/discuss/90908/easy-python-o-n-solution-94%25-with-explanations
if numRows == 1:
return s
# calculate period
p = 2 * (numRows - 1)
result = [""] * numRows
for i in xrange(len(s)):
floor = i % p
if floor >= p//2:
floor = p - floor
result[floor] += s[i]
return "".join(result)
if __name__ == '__main__':
# begin
s = Solution()
print s.convert("PAYPALISHIRING", 3)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.