zoukankan      html  css  js  c++  java
  • ZigZag Conversion(Z字转换)

     ZigZag Conversion 

    Leetcode 的这个题目难主要难在对于题目的了解上,什么才是Z字格式排列呢?

    带着问题我们来继续看ABCDEFGHIJK的排列

    A        G
    B   F   H
    C   E   I   K
    D        J

    示例中的三行在加上这里的四行示例,我想大家都明白了什么是Z字格式了吧。

    代码实现也就很简单了,经参考下面的代码

     1 var strList = new List<string>(numRows);
     2 
     3             if (s.Length <= 1 || s.Length < numRows)
     4             {
     5                 return s;
     6             }
     7             for (int i = 0; i < numRows; i++)
     8             {
     9                 strList.Add(string.Empty);
    10             }
    11             int rowNum = 1;
    12             int flag = 1;
    13             for (int i = 0; i < s.Length; i++)
    14             {
    15                 strList[rowNum - 1] += s[i];
    16                 if (rowNum == numRows)
    17                 {
    18                     flag = -1;
    19                 }
    20                 if (rowNum == 1)
    21                 {
    22                     flag = 1;
    23                 }
    24                 rowNum += flag;
    25             }
    26             var temp = string.Empty;
    27             foreach (var item in strList)
    28             {
    29                 temp += item;
    30             }
    31             return temp;
    View Code

    利用一个数组就很容易的解决问题

  • 相关阅读:
    Redis源码阅读笔记(2)——字典(Map)实现原理
    Partition List ——LeetCode
    docker format (golang template)
    markdown 换行
    pecan快速教程
    nvdimm
    k8s device plugin
    linux 文件同步
    复制MIFARE Classic卡
    install docker swarm on centos
  • 原文地址:https://www.cnblogs.com/tomguo/p/8478912.html
Copyright © 2011-2022 走看看