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. 【POJ 1035】Spell checker

    题 题意 每个单词,如果字典里存在,输出”该单词 is correct“:如果字典里不存在,但是可以通过删除.添加.替换一个字母得到字典里存在的单词,那就输出“该单词:修正的单词”,并按字典里的顺序输 ...

  2. Oracle自定义函数实例

    1. 传入一个值, 如果该值为0,则返回空. CREATE OR REPLACE FUNCTION Fun_Test(p IN NUMBER) RETURN VARCHAR2 IS v_Result ...

  3. [NOIP2011] 提高组 洛谷P1311 选择客栈

    题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...

  4. Android 设计模式 之 单例模式

    http://blog.csdn.net/fangchongbory/article/details/7734199   目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ...

  5. Redis 集合操作

    1.SCARD key 返回集合 key 的基数(集合中元素的数量). 2.SDIFFSTORE destination key [key ...] 这个命令的作用和  类似,但它将结果保存到 des ...

  6. 轻量级应用开发之(07) UIPickerView使用

    #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPicker ...

  7. poj1631Bridging signals(最长单调递增子序列 nlgn)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12251   Accepted: 6687 ...

  8. [Angularjs]国际化

    写在前面 在项目中,有用到国际化,跟着就了解了下使用angularjs实现的国际化,这里做一下记录. 系列文章 [Angularjs]ng-select和ng-options [Angularjs]n ...

  9. FileStream文件流的读取和写入(为以后聊天工具的设计基础)

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  10. centos 配置固定ip

    centos下网络配置方法(网关.dns.ip地址配置) 来源:互联网 作者:佚名 时间:07-13 00:32:07 [大 中 小] 本文介绍了centos网络配置的方法,centos网络配置主要包 ...