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. tarball文件安装的大概流程

    ./configure这个步骤就是在创建 Makefile 这个文件罗!通常程序开发者会写一支 scripts 来检查你的 Linux 系统.相关的软件属性等等,这个步骤相当的重要, 因为未来你的安装 ...

  2. Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例

    1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...

  3. PHP中的超级全局变量

    PHP内置了一些超级全局变量,我们可以在脚本的任何地方使用和可见,下面记录一下这些全局变量的作用: 1.$_SERVER $_SERVER超级全局变量包含由web服务器创建的信息,它提供了服务器和客户 ...

  4. 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法

    不论下方提示什么数据格式有错误,一般都是entity生成的时候的问题.比如服务器上用的sql2005,自己用的2008. 解决方法: model层生成的model.edmx文件,用记事本打开, 将&l ...

  5. HttpServlet was not found on the Java

    今天新建jsp时出现了一个错误,如下图 分析:应该是没有找到相关jar包 解决方案: 如图: 这回就没错了

  6. DOS 全集

       DOS全集 winver 检查Windows版本 wmimgmt.msc 打开Windows管理体系结构(wmi) wupdmgr Windows更新程序 wscript Windows脚本宿主 ...

  7. meteor中分页库alethes:pages用法汇总

    1.添加分页库: meteor add alethes:pages 2.新建分页: Pages = new Meteor.Pagination("collection-name") ...

  8. jQuery对象和Dom对象的区分以及之间转换

    刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 一,什么是jQuer ...

  9. ios UIWebview本地加载H5网页

    注意两点 1.拖动文件到工程中选择create folder,文件夹为蓝色  --不要让文件参与编译,而只是让文件加入进来 2.加载方式pathforresorth   oftype   indire ...

  10. [C#][转][string 字符串截取

    C#几个经常用到的字符串截取 一. 1.取字符串的前i个字符 (1)string str1=str.Substring(0,i); (2)string str1=str.Remove(i,str.Le ...