leetcode第六题 ZigZag Conversion (java)
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 N
A P L S I I G
Y I R
And 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".
思路:列输入-->行输出,将zigzag字符串看成是字符数组,为空的位置其值为null
先确定数组的列数(nRows即为数组的行数)
time=440ms
accepted
public class Solution {
public String convert(String s, int nRows) {
int length=s.length();
if(length<=nRows||nRows==1)
return s;
int updown=0,i=0,j=1,count=0,nLines=0;
while(count<length){
if(updown==0){
if(i>nRows-1){
updown=nRows-1;
i=nRows-2;
}else{
count++;
i++;
}
}
if(updown==nRows-1){
if(i<1){
updown=0;
i=0;
j++;
}else{
count++;
i--;
j++;
}
}
}
nLines=j;
System.out.println("j="+j);
char[][] zigzag=new char[nRows][nLines];
updown=0;i=0;j=0;count=0;
while(count<length){
if(updown==0){
if(i>nRows-1){
updown=nRows-1;
i=nRows-2;
j++;
}else{
zigzag[i][j]=s.charAt(count);
count++;
i++;
}
}
if(updown==nRows-1){
if(i<1){
updown=0;
i=0;
}else{
zigzag[i][j]=s.charAt(count);
count++;
i--;
j++;
}
}
}
StringBuffer sb = new StringBuffer();
for(int m=0;m<nRows;m++)
for(int n=0;n<nLines;n++){
if(zigzag[m][n]!='\0'){
sb.append(String.valueOf(zigzag[m][n]));
}
}
return sb.toString();
}
}
leetcode第六题 ZigZag Conversion (java)的更多相关文章
- leetcode第六题--ZigZag Conversion
Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...
- LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)
题目描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- leetcode 6. ZigZag Conversion [java]
自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode(6) ZigZag Conversion
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- LeetCode(6)ZigZag Conversion
题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- 路径MTU
数据在以太网中的传输有长度有一个限制,其最大值一般情况下是1500字节.链路层的这个特性叫作MTU,也就是最大传输单元.不同类型的网络会有所不同的.如果IP层有一个数据报要传输,而且数据的长度比链路层 ...
- launchMode使用详解
launchMode是很基础但是也很容易被忽视的问题,一个高性能的手机App不仅仅是代码完成的非常棒,也包括launchMode的合理使用.一个应用中,到底哪些Activity应该始终保持一个实例,哪 ...
- Android_layout_note
LinearLayout线程布局 LinearLayout属性 android:orientation表示线性布局的方向 vertical: 垂直.从上往下 horizontal: 水平.从左往右 a ...
- 通过jQuery的attr修改onclick值的的解决方法
好了,直接给大家贴上js代码吧 var js = "alert('B:' + this.id); return false;"; var newclick = eval(" ...
- mysql查询练习
mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price, ...
- VC++ Bresenham画线实例
附带百度链接:http://wenku.baidu.com/link?url=GP4uDkoyulgNxQy5djBBi-JB5BCrMWW6svMDhSfmzi_Qi1s6DhwJiCPHdMI2o ...
- datejs lib
// Get today's date Date.today(); // Add 5 days to today Date.today().add(5).days(); // Get Friday o ...
- Android 抽屉效果
昨天在用“酷我音乐”听歌的时候注意到了界面右上角的四角方块,当我点击这个方块的时候会从屏幕的左边弹出新的界面而把原来的界面挤到左边,是显示了一小部分. 于是,我便在网上查询了一下相关的文章,现将这种效 ...
- sqlserver中的锁
NOLOCK(不加锁) 此选项被选中时,SQL Server 在读取或修改数据时不加任何锁. 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Roll ...
- [python]pep8编码规范
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...