【Leetcode】zigzagConversion JAVA
来源:互联网 发布:国内域名有哪些 编辑:程序博客网 时间:2024/05/17 01:28
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"
.
For the record...what if 4 and 5:
n=4
P I N
A L S I G
Y A H R
P I
N=5
P H
A S I
Y I R
P L I G
A N
//P I N//A L S I G//Y A H R//P Ipublic class zigzagConversion {public static void main(String args[]){zigzagConversion zz = new zigzagConversion();System.out.println(zz.convert("paypalishiring",4));}public String convert(String s, int nRows) {if(s == null || s.length()==0 || nRows <=0) return ""; if(nRows == 1) return s; StringBuilder res = new StringBuilder(); int size = 2*nRows-2; for(int i=0;i<nRows;i++) { for(int j=i;j<s.length();j+=size) { res.append(s.charAt(j)); if(i!=0 && i!=nRows-1 && j+size-2*i<s.length()) { System.out.println("size:"+size+" i:"+i+" j:"+j+" is "+s.charAt(j+size-2*i)); res.append(s.charAt(j+size-2*i)); } } } return res.toString(); }}
0 0
- 【Leetcode】zigzagConversion JAVA
- LeetCode java实现ZigZagConversion
- leetcode #6 ZigZagConversion
- Leetcode——ZigZagConversion
- LeetCode 006 ZigZagConversion
- ZigZagConversion
- 算法--ZigZagConversion
- leetcode6 zigzagConversion
- Leetcode(java)
- leetcode java
- [LeetCode]Next Permutation java leetcode
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- [Leetcode] ZigZag Conversion (Java)
- [Leetcode] Reverse Integer (Java)
- [Leetcode] Palindrome Number (Java)
- [Leetcode] Valid Parentheses (Java)
- [Leetcode] Generate Parentheses (Java)
- Win8.1(64bit)+VS2012+OpenCV2.4.9(32位和64位)正确配置
- 移动应用开发中如何做数据缓存
- Monte Carlo方法的基本思路
- UDK:UDK 常用快捷键
- 创建SVN分支
- 【Leetcode】zigzagConversion JAVA
- 非接触式IC卡、条码卡、磁道卡、接触式IC卡、芯片卡
- java环境变量配置
- LeetCode OJ 之 Remove Element (删除指定元素)
- 23种设计模式之--简单工厂模式
- hdu1540 线段树区间合并
- Sum Root to Leaf Numbers
- HDU 4786(最小生成树 kruskal)
- 给予下列一串字符串a,b,c,ab,bb,cb,ac,bc,cc,aab,bab,cab,abb,bbb,cbb...通过编程实现以上字符串实现,并要求有相应字符串的位置信息,假设a的位置为0