zoukankan      html  css  js  c++  java
  • leetcode——6.Z字形变换

    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows==1:
                return s
            if len(s)<2:
                return s
            s1=s[::numRows*2-2]
            d=[i for i in range(len(s))][::numRows*2-2]
            s2=s[numRows-1:][::numRows*2-2]
            d_1=[i+numRows-1 for i in d]
            if len(s1)==len(s2):
                d=d_1
                t=numRows-2
                while t>0:
                    d1_1=[i+t for i in d if i+t<len(s)]
                    d1_2=[i-t for i in d if i-t>0]
                    d1=d1_1+d1_2
                    d1.sort()
                    s3=''
                    for i in range(len(d1)):
                        s3=s3+s[d1[i]]
                    s1=s1+s3
                    t-=1
            elif len(s1)>len(s2):
                t=1
                while t<=numRows-2:
                    d1_1=[i+t for i in d if i+t<len(s)]
                    d1_2=[i-t for i in d if i-t>0]
                    d1=d1_1+d1_2
                    d1.sort()
                    s3=''
                    for i in range(len(d1)):
                        s3=s3+s[d1[i]]
                    s1=s1+s3
                    t+=1
            return s1+s2
    执行用时 :80 ms, 在所有 Python 提交中击败了36.02%的用户
    内存消耗 :11.7 MB, 在所有 Python 提交中击败了45.14%的用户
     
    好笨哦,两个小时才编出来这么一个。。。
    执行用时为 24 ms 的范例
    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if numRows<2 :
                return s
            result = ['' for i in range(numRows)]
            j = 0
            for i in range(len(s)):
                result[j] += s[i]
                if j == numRows-1:
                    bool = -1
                if j == 0:
                    bool = 1
                j += bool
            return ''.join(result)

    人家这个办法好妙啊!!!!!!!!!

                                                                     ——2019.10.11


    public String convert(String s, int numRows) {  //找规律
            char[] c = s.toCharArray();
            int len = s.length();
            StringBuilder sb = new StringBuilder();
            if(s .equals("") || numRows == 1 || numRows >= len) return s;
            for(int i = 0;i<numRows;i++){
                int index = i;
                sb.append(c[i]);
                while(index<len){
                    if(i!=numRows-1) {
                        index += (numRows - i - 1) * 2;
                        if (index < len) {
                            sb.append(c[index]);
                        } else {
                            break;
                        }
                    }
                    if(i!=0) {
                        index += i * 2;
                        if (index < len) {
                            sb.append(c[index]);
                        } else {
                            break;
                        }
                    }
                }
            }
            return sb.toString();
        }

    我的天,,我现在已经看不懂python代码了。。。

     ——2020.9.1

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    第一次会议(2019.3.4)
    改革春风吹满地小组~~成立了~~⭐😄~~
    PYQT5 系列(一)——参考自《弗兰克万岁》
    Springboot2.0学习笔记1__微服务及springboot的作用
    Java学习之---------------反射
    Jquery对select的操作 添加一个select
    匿名函数
    数据库迁移
    EF 未应用自动迁移,因为自动迁移会导致数据丢失的解决办法
    在Chrome+Visual Studio中调试asp.net程序很慢的问题(Firefox也有类似问题)
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11654763.html
Copyright © 2011-2022 走看看