LeetCode #6 ZigZag Conversion C# Solution
2016-04-16 10:25
567 查看
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
可能有人不知道ZigZag是什么意思,其实就是给一个字符串,让你按照Z字形来排列,同时要求按给出的行数排列,最后按行输出。
其实就是找规律,向下循环是nRows,斜角线循环是nRows-2。
可能有人不知道ZigZag是什么意思,其实就是给一个字符串,让你按照Z字形来排列,同时要求按给出的行数排列,最后按行输出。
其实就是找规律,向下循环是nRows,斜角线循环是nRows-2。
C# Code public class Solution { public string Convert(string s, int numRows) { if (s.Length == 0 || numRows < 2) return s; string ans = ""; int time = 2 * (numRows - 1); for (int i = 0; i < numRows; i++) { for (int j = i; j < s.Length; j += time) { ans += s[j]; if (i > 0 && i < numRows - 1) { if (j + time - 2 * i < s.Length) { ans += s[j + time - 2 * i]; } } } } return ans; } }
相关文章推荐
- default 关键字泛型代码中的默认关键字(C# 编程指南)
- C#实现软件开机自启动原理与代码
- C#编程基础笔记-FileDialog
- C#一般处理程序设置和读取session(session报错未将对象引用设置到对象的实例解决)
- .NET设计模式(1):1.1 单例模式(Singleton Pattern)
- 设计模式(Design Pattern)系列之.NET专题
- WinForm全屏应用程序
- C#杀死Explorer(资源管理器)
- c#复制包含子目录文件夹代码
- 值类型后面加问号
- c#封装4x4的矩阵
- C#如何在钉钉开发平台
- C#—密度较量拓展
- C# protobuf的使用方法
- C#—Lambda表达式用法
- C#—拓展方法
- C#—匿名方法和静态类使用
- 用C#创建XML文档和读取并修改XML文档
- C# 扩展方法验证居民身份证
- C#时间格式化(Datetime)用法详解