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"

解题思路:

观察题目,靠眼力寻找规律即可。如果没有读懂ZigZag的话,请移步

http://blog.csdn.net/zhouworld16/article/details/14121477

java实现:

static public String convert(String s, int nRows) {
if (s == null || s.length() <= nRows || nRows <= 1)
return s; StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i += (nRows - 1) * 2)
sb.append(s.charAt(i)); for (int i = 1; i < nRows - 1; i++) {
for (int j = i; j < s.length(); j += (nRows - 1) * 2) {
sb.append(s.charAt(j));
if (j + (nRows - i - 1) * 2 < s.length()) {
sb.append(s.charAt(j + (nRows - i - 1) * 2));
}
}
} for (int i = nRows - 1; i < s.length(); i += (nRows - 1) * 2)
sb.append(s.charAt(i)); return new String(sb);
}

C++实现如下:

 #include<string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if (s.length() <= numRows || numRows <= )
return s; string sb;
for (int i = ; i < s.length(); i += (numRows - ) * )
sb+=s[i];
for (int i = ; i < numRows - ; i++) {
for (int j = i; j < s.length(); j += (numRows - ) * ) {
sb+=s[j];
if (j + (numRows - i - ) * < s.length())
sb+=s[j + (numRows - i - ) * ];
}
} for (int i = numRows - ; i < s.length(); i += (numRows - ) * )
sb += s[i];
return sb;
}
};

【JAVA、C++】LeetCode 006 ZigZag Conversion的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  7. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

随机推荐

  1. Java编程思想学习(二) 操作符

    1. 对象“赋值”:对一个对象进行操作时,我们真正操作的是对对象的引用.所以倘若“将一个对象赋值给另一个对象”,实际是将“引用”从一个地方复制到另一个地方.(引用于对象之间存在关联,但这种关联可以被改 ...

  2. c++ 函数调用在进入下一个循环的时候会再次初始化参数,将函数体直接写进去就正常

    #include"stdafx.h" #include"string" #include<iostream> #include<vector& ...

  3. POJ1088滑雪(dp+记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 86411   Accepted: 32318 Description ...

  4. linux:Nginx+https双向验证(数字安全证书)

    本文由邓亚运提供 Nginx+https双向验证 说明: 要想实现nginx的https,nginx必须启用http_ssl模块:在编译时加上--with-http_ssl_module参数就ok.另 ...

  5. MyEclipse------各种问题解决方法

    1.汉化后如何变为英文版:找到myeclipse.ini文件,改为:language=enlanguage=zh为中文 2.解决版本不匹配问题:http://blog.sina.com.cn/s/bl ...

  6. yii框架常用url地址

    调用YII框架中 jquery:Yii::app()->clientScript->registerCoreScript('jquery');        framework/web/j ...

  7. linux4

    linux 特点:1.免费 开源(代码公开)2.支持多线程/多用户的操作系统3.安全性4.对内存和文件管理有自己的一套优越的方法 linux最小只需要4M ->嵌入式开发默认不启动用户界面roo ...

  8. thinkphp开发规范

    1.编写目的     为了更好的提高技术部的工作效率,保证开发的有效性和合理性,并可最大程度的提高程序代码的可读性和可重复利用性,指定此规范.开发团队根据自己的实际情况,可以对本规范进行补充或裁减. ...

  9. Linux之Sed命令详解(总结一些实用例子)

    简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...

  10. go runtime scheduler

     http://www.slideshare.net/matthewrdale/demystifying-the-go-scheduler http://www.cs.columbia.edu/~a ...