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)的更多相关文章

  1. leetcode第六题--ZigZag Conversion

    Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...

  2. LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)

    题目描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  3. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  4. leetcode 6. ZigZag Conversion [java]

    自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows ...

  5. LeetCode之“字符串”:ZigZag Conversion

    题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  6. LeetCode(6) ZigZag Conversion

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. LeetCode(6)ZigZag Conversion

    题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...

  8. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  9. 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 ...

随机推荐

  1. redis单机及其集群的搭建

    http://www.cnblogs.com/mouseIT/p/5288204.html

  2. Thread和Runnable、run和start的区别

    多线程可以通过两种方式来创建: 一.通过继承Thread类. 二.通过实现Runnable接口. 那么中两种方式到底有什么区别呢?那种方式更好些呢? 先看看几个简单的Demo: Demo1 publi ...

  3. 台湾书籍代购网址——2013-08-25 16

    台湾书籍代购 博客来http://www.books.com.tw 三民http://www.sanmin.com.tw 诚品http://www.eslite.com 金石堂http://www.k ...

  4. linux shell执行方式

    linux shell执行有两种方式 shell脚本以#!/bin/bash开头,执行shell时先检查首行,在内部以下列方式执行: $/bin/bash script.sh 1. 使用sh执行. $ ...

  5. mac 终端中添加tree命令显示文件目录结构

      在Ubuntu下,通过 sudo apt-get install tree 可以使用tree命令,显示文件目录列表,如图所示: 在mac OS X系统下怎么使用呢? 在终端输入: cd $home ...

  6. Java并发——Fork/Join框架

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4631466. ...

  7. StyleCop 安装

    下载安装 官方网站:http://stylecop.codeplex.com/ 下载安装 如果自定义了安装路径请保证这个文件的AssemblyFile 属性正确的指向了StyleCop.dll所在的目 ...

  8. Android 里面的小坑

    1.listview加了个blockdescen,竟然导致editTextView不能获取焦点

  9. Linq 与UnitOfWork

    submitchages(linq to sql)或者savechanges(ef)的次数是根据你操作方法的数量决定的,也即是:它只认识自己的提交语句(submtchanges,savechanges ...

  10. requirejs实践二 加载其它JavaScript与运行

    上一篇中介绍了requirejs加载JavaScript文件,在这一篇中介绍加载JavaScript后执行代码 这次是test2.html文件, <!DOCTYPE html> <h ...