博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZigZag Conversion
阅读量:7030 次
发布时间:2019-06-28

本文共 1721 字,大约阅读时间需要 5 分钟。

题目

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   NA P L S I I GY   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"
.

方法

将字符串Z字形排列,然后输出新的字符串。

仅仅须要循环处理就可以。递增在递减。

public String convert(String s, int nRows) {	        String[] subString = new String[nRows];	        	        for(int i =0; i < nRows; i ++){	            subString[i] = "";	        }	        	        int flag = 0;	        int currentRows = 0;	        String convertString ="";	        if(nRows == 1){	            return s;	        }	        for(int i = 0; i < s.length(); i ++){	            if(flag == 0){	                if(currentRows < nRows){	                    subString[currentRows] +=s.charAt(i);	                    currentRows ++;	                }	                if(currentRows == nRows){	                    flag = 1;	                    currentRows -- ;	                }	                	            }else{	                if(currentRows > 0){	                    subString[currentRows - 1] +=s.charAt(i);	                    currentRows --;	                }	                if(currentRows == 0){	                    flag = 0;	                    currentRows ++;	                }	            }	        }	        	        	        	        for(int i = 0; i < nRows; i ++){	            convertString += subString[i];	        }	        return convertString;	    }

转载于:https://www.cnblogs.com/clnchanpin/p/6937486.html

你可能感兴趣的文章
css attr 利用特殊字体 图片 :before content
查看>>
移动平台3G手机网站前端开发布局技巧汇总(转)
查看>>
2018-2019-1 20165231《信息安全系统设计基础》第一周学习总结
查看>>
VSSより、指定したファイルを取得するマクロ(パス入り)
查看>>
Android控件第7类——对话框
查看>>
执行JS
查看>>
学校中有老师和学生两类人,而在职研究生既是老师又是学生,对学生的管理和对教师的管理在他们身上都有体现。...
查看>>
黑马程序员——JAVA学习笔记七(String类)
查看>>
20135202闫佳歆20135220谈愈敏实验一
查看>>
CodeForces 678D Iterated Linear Function
查看>>
ACdream 1083 人民城管爱人民
查看>>
内存缓存memory-cache
查看>>
MultiByteToWideChar
查看>>
centos7下zookeeper集群安装部署
查看>>
Code Generation => Table -> Class for DataGridView use
查看>>
CSS预处理器——Sass、LESS和Stylus实践【未删减版】
查看>>
centos7 hdfs yarn spark 搭建笔记
查看>>
spring boot中的jave注解学习
查看>>
Java -- 浅入Java反射机制
查看>>
c# Dictionary 简介
查看>>