LeetCode - ZigZag Conversion
来源:互联网 发布:linux 挂载有数据硬盘 编辑:程序博客网 时间:2024/06/02 05:45
https://leetcode.com/problems/zigzag-conversion/
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)
P A H NA P L S I I GY I RAnd then read line by line:
"PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.这道题只要看出规律是按 2×nRows-2重复就行,然后按照规律再每行依次打印就可以了。
public class Solution { public String convert(String s, int nRows) { if(s==null || s.length()==0 || nRows<=0) return ""; if(nRows == 1) return s; int size = 2*nRows - 2; StringBuilder sb = new StringBuilder(); for(int i=0; i<nRows; i++){ for(int j=i; j<s.length(); j+= size){ sb.append(s.charAt(j)); int next = (size - j%size) + size * (j/size); if(i!=0 && i!=(nRows-1) && next<s.length()) sb.append(s.charAt(next)); } } return sb.toString(); }}
0 0
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- [LeetCode] Zigzag Conversion
- LeetCode 6 - ZigZag Conversion
- Leetcode : ZigZag Conversion
- 【leetcode】Zigzag Conversion
- [LeetCode]ZigZag Conversion
- leetcode ZigZag Conversion
- LeetCode-ZigZag Conversion
- LeetCode ZigZag Conversion
- LeetCode 6: ZigZag Conversion
- leetcode-006:ZigZag Conversion
- Leetcode: ZigZag Conversion
- [LeetCode]ZigZag Conversion
- [LeetCode] ZigZag Conversion
- [LeetCode]ZigZag Conversion
- leetcode: ZigZag Conversion
- 03-树1. List Leaves (25)
- 【hadoop】 3004-hadoop序列化机制思想
- C语言知识结构图(用mindmap生成)
- #笔记#圣思园 JavaSE 第10讲——面向对象之 封装 encapsulation 续
- 【hadoop】 3005-hadoop对象序列化编码
- LeetCode - ZigZag Conversion
- #3 Longest Substring Without Repeating Characters(最长无重复元素子串)
- Android 报Error inflating class ...错误
- jdk与jre的区别
- Aliyun Linux 安装 Apache 步骤
- Obj-C中的不可变数组NSArray常用方法
- 3. 过滤和排序数据
- [第一章]初探JavaScript,命名规则,弹出对话框,以及JavaScript的引用
- [nginx] 对UA为空的请求返回403