From a628a44c280040d5c1a333fc4257a77a9caa7f90 Mon Sep 17 00:00:00 2001 From: uwivbn Date: Wed, 28 Aug 2019 21:03:13 +0800 Subject: [PATCH] =?UTF-8?q?Z=E5=AD=97=E5=BD=A2=E5=8F=98=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file_code.py" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "6\343\200\201Z\345\255\227\345\275\242\345\217\230\346\215\242/file_code.py" diff --git "a/6\343\200\201Z\345\255\227\345\275\242\345\217\230\346\215\242/file_code.py" "b/6\343\200\201Z\345\255\227\345\275\242\345\217\230\346\215\242/file_code.py" new file mode 100644 index 0000000..6a26fe0 --- /dev/null +++ "b/6\343\200\201Z\345\255\227\345\275\242\345\217\230\346\215\242/file_code.py" @@ -0,0 +1,59 @@ +from collections import defaultdict + +class Solution: + def convert(self, s: str, numRows: int) -> str: + if s == '' or numRows == 1: + return s + len_str = len(s) + base = 2*(numRows-1) + nums = len_str // base + base_ind = [x for x in range(numRows)] + + minis = len_str % base + print(nums) + print(base_ind) + print(base) + print(minis) + arr = defaultdict(list) + for i in range(nums): + for j in base_ind: + if j in [base_ind[-1], base_ind[0]]: + arr[j].append(j+base*i) + else: + arr[j].extend([j+base*i, j+base*(i+1)-2*j]) + + flag = nums * base + if minis != 0: + if minis // numRows == 0: + for i in range(0, minis): + arr[i].append(flag+i) + else: + for i in range(0, numRows): + arr[i].append(flag+i) + for i, val in enumerate(range(flag+numRows, len_str), 1): + arr[numRows-i-1].append(val) + print(val) + + arr_str = defaultdict(list) + for k in arr.keys(): + for i in arr[k]: + arr_str[k].append(s[i]) + + return ''.join([''.join(val) for val in arr_str.values()]) + + + +if __name__ == "__main__": + solution = Solution() + string = 'LEETCODEISHIRING' + numRows = 4 + # result = 'LCIRETOESIIGEDHN' + + ''' + string = 'LEETCODEISHIRING' + numRows = 3 + # result = 'LDREOEIIECIHNTSG' + ''' + result = solution.convert(string, numRows) + + print(result)