LeetCode 6. ZigZag Conversion
来源:互联网 发布:柯林斯 知乎 编辑:程序博客网 时间:2024/06/11 13:19
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"
.
转载自 点击打开链接
public class Solution { public String convert(String s, int numRows) { char[] ss =s.toCharArray(); if(numRows<=1){ return s; }else if(numRows>=s.length()){ return s; }else{ StringBuffer result=new StringBuffer(); int tap= 2*numRows-2; //先添加第一行的字符 for(int i=0;i<s.length();i+=tap){ result.append(ss[i]); } //然后添加中间行 int temp = tap -2; for(int i=1;i<numRows-1;i++){ for(int j=i;j<s.length();j+=tap) { result.append(ss[j]); if(j+temp<s.length()){ result.append(ss[j+temp]); } } temp-=2; } //添加最后一行 for(int i=numRows-1;i<s.length();i+=tap){ result.append(ss[i]); } return result.toString(); } }}
阅读全文
0 0
- LeetCode 6. ZigZag Conversion
- LeetCode --- 6. ZigZag Conversion
- LeetCode 6.ZigZag Conversion
- [Leetcode] 6. ZigZag Conversion
- 【leetcode】6. ZigZag Conversion
- [LeetCode] 6.ZigZag Conversion
- LeetCode - 6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- Leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- Leetcode 6. ZigZag Conversion
- [leetcode]6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- LeetCode 6. ZigZag Conversion
- LeetCode *** 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- i2c驱动程序(2) i2c_driver probe被调用的流程分析
- Qoo10选品数据宝典,小卖家与新手的必备武器
- HTML之一些效果的实现
- awk使用shell变量及shell使用awk中的变量
- TensorFlow番外篇——module 'tensorflow' has no attribute 'xxx'
- LeetCode 6. ZigZag Conversion
- Android5.0隐式启动服务
- Nodejs之静态资源处理
- MySQL学习笔记---基础概念和一些基础SQL语句(一)
- 爬虫爬取网页,并用浏览器打开
- Ubuntu语言设置汉语无法移动
- Hibernate 什么梗
- 魔方数
- 卡通渲染简报