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. 图解Android - 如何看Android的UML 图?

    如何看UML图? UML能给我们带来什么? 这是本文要寻找的答案.UML图有很多类型,我们这里只讨论最重要也最常用的两种 - 类图和时序图. 1. 类图 通过类图,我们可以很容易的了解代码架构,理清模 ...

  2. 【CodeForces 604B】F - 一般水的题1-More Cowbe

    Description Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter ...

  3. Myeclipse快捷键的使用

    存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shi ...

  4. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  5. BZOJ1015 [JSOI2008]星球大战starwar

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的 机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...

  6. Matlab中的括号()[]{}

    Matlab中的括号()[]{} Matlab中经常会用到括号去引用某Array或者是cell的内容,但三者有什么具体区别呢?[ ] 中括号用来构建向量(Vectors)或者是矩阵(Matrices) ...

  7. Python socket编程之六:多窗口的应用

    import struct import sqlalchemy import pandas import matplotlib.pyplot as Plot from matplotlib.finan ...

  8. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  9. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  10. Solr之搭建Solr5.2.1服务并从Mysql上导入数据

    原文地址:http://www.bkjia.com/webzh/1026243.html