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. careercup-树与图 4.2

    4.2 给定有向图,设计一个算法,找出两个结点之间是否存在一条路径. 解答 根据题意,给定一个有向图和起点终点,判断从起点开始,是否存在一条路径可以到达终点. 考查的就是图的遍历,从起点开始遍历该图, ...

  2. qt 共享内存(QSharedMemory)

    ——————————————————写入部分—————————————————— (本次程序基于控制台程序) 首先 使用共享内存得召唤一下: #include <QSharedMemory> ...

  3. JsonUtil对象与json互转

    OrderDto orderDto = JsonUtil.json2Object(json,     new TypeRef<OrderDto>() {     }); package c ...

  4. jQuery多图上传Uploadify插件使用及传参详解

    因为工作需要,这两天接触到了Uploadify插件,由于是第一次用,花了我近一天的时间.下面我把我在用这个插件过程详细的分享出来,也让自己巩固一下,也希望能帮助到你. 所需文件: jquery-1.8 ...

  5. Hashtable和HashMap类

    Hashtable和HashMap类有三个重要的不同之处. 第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现 ...

  6. andorid 平台调用Web Service , 图片传输

    今天学习了下android调用web service,进行图片传输 下面是代码详解: onActivityResult 方法在图片剪裁完成之后调用: protected void onActivity ...

  7. 使用ROW_NUMBER进行的快速分页

    DECLARE @pageSize INT ; DECLARE @pageIndex INT ; SET @pageSize = 5 SET @pageIndex =2 ; --第二页,每页显示5条数 ...

  8. WEB开发时Browser控件得到C:\fakepath\ 的解决方式

    IE9中JS获得文件上传控件的路径不对,为:C:\fakepath\ 原来要修改:  工具 -> Internet选项 -> 安全 -> 自定义级别 -> 将本地文件上载至服务 ...

  9. 使用hwclock同步RTC(硬件时钟)和OS Clock(操作系统时钟)

    Linux下面有个命令叫做hwclock,其主要的功能是用来在RTC和操作系统之间进行时钟同步.既可以将RTC的时钟同步到操作系统,也可以在通过hwclock将当前系统时间.Internet时间.本地 ...

  10. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...