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 ...
随机推荐
- 从零开始,打造自己的首个 iOS 框架
如果你曾试图创建自己的iOS框架,你知道这不是一个头脑发热作出的决定 — 管理依赖以及写测试用例一点也不简单.本教程将会带你从头到尾创建你的第一个iOS框架,让你可以创建自己的框架. 我们将在框架暴露 ...
- Android Activiy的作用
在Android应用程序中 ,Activity主要的负责创建窗口的,一个Activicy就是代表一个单独的屏幕,并且是用户唯一可以看到的东西 也就是说Activity就是用来实现和用户交互的,就和.n ...
- Nginx高性能服务器安装、配置、运维 (1) —— Nginx简介
一.Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx特点 ...
- 使用modelsim仿真DDR3时编译出错的解决方法
Modelsim 10.1c release note sates as : Product Changes in 10.1c Release 10.1b introduced a new error ...
- java Spring配置数据单元
基本原理 - 容器和bean 在Spring中,那些组成你应用程序的主体(backbone)及由Spring IoC容器所管理的对象,被称之为bean. 简单地讲,bean就是由Spring容器初始化 ...
- c语言学习之基础知识点介绍(十七):写入读取结构体、数组、结构体数组
一.结构体的写入和读取 //写入结构体 FILE *fp = fopen("/Users/ios/Desktop/1.data", "w"); if (fp) ...
- MVC Filter自定义验证(拦截)
namespace QS.Web.Extensions { /// <summary> /// 验证session.权限 状态 /// </summary> [Attribut ...
- jsp 页面获取xml的内容
<c:out value="${history.xml}" escapeXml="true" />
- mysql相关重要问题解决
root密码修改 MySQL 的管理员密码: sudo mysqladmin -u root password newpassword: mysql无法安装:删除/etc/mysql, /var/ ...
- 近期专案PM相关收获
1, 厚黑学讲的有道理, 坏人? 为什么占便宜., 好人为什么当不了坏人是有一定道理的. -- 作为PM,能力大小居然都能胜任,从这一点上对组员不负责,如下种种都算有则改之无则加勉. ...