zoukankan      html  css  js  c++  java
  • leetcode6:Z字形变换

    =============================Python==========================

    class Solution:
        def convert(self, s: str, numRows: int) -> str:
            res = ['' for _ in range(numRows)]
            i = 0
            while i < len(s):
                j = 0
                while i < len(s) and j < numRows:
                    res[j] += s[i]
                    i += 1
                    j += 1
                k = numRows - 2
                while i < len(s) and k > 0:
                    res[k] += s[i]
                    i += 1
                    k -= 1
            ans = ''
            for i in res:
                ans += i
            return ans

    ==============================Java===========================

    class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) return s;
            List<StringBuilder> rows = new ArrayList<>();
            for (int i = 0; i < Math.min(numRows, s.length()); i++){
                rows.add(new StringBuilder());
            }
            int curRow = 0;
            boolean goingDown = false;
            for (char c : s.toCharArray()) {
                rows.get(curRow).append(c);
                if (curRow == 0 || curRow == numRows - 1) {
                    goingDown = !goingDown;
                }
                curRow += goingDown ? 1 : -1;
            }
    
            StringBuilder ret = new StringBuilder();
            for (StringBuilder row : rows) ret.append(row);
            return ret.toString();
    
        }
    }
  • 相关阅读:
    Ajax跨域解决实例
    关于tween.js测试介绍
    signal() 和 sigaction()
    信号概述
    监控文件事件
    栈和栈帧
    进程结构和内存布局
    关于文件I/o的原子操作
    查询Linux系统中glibc的版本
    IOPS和Throughput
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13493884.html
Copyright © 2011-2022 走看看