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. 【kAri OJ605】陈队的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 陈队有N棵树,有一天他突然想修剪一下这N棵树,他有M个修剪器,对于每个修剪器给出一个高度h,表示这个修剪器可以把某一棵高度超过h的树修剪 ...

  2. 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛

    4407: 于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 241  Solved: 119[Submit][Status][Discu ...

  3. Azure怎么使用ftp登录

    1.下载配置文件 2.拷贝FTP的地址 3.查看配置文件里面的用户名和密码 4.登录

  4. 车牌号对应归属地及城市JSON带简码

    车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...

  5. [Angularjs]单页应用之分页

    写在前面 在项目中,用到单页应用的分页,当时想到使用滚动加载的方案,可是几次尝试都没配置成功,闲着无聊就弄了一个demo. 系列文章 [Angularjs]ng-select和ng-options [ ...

  6. Hadoop的datanode无法启动

    Hadoop的datanode无法启动 hdfs-site中配置的dfs.data.dir为/usr/local/hadoop/hdfs/data 用bin/hadoop start-all.sh启动 ...

  7. 实时获取UITextField内容

    在UISearchBar中,当输入信息改变时,它就会调用textDidChange方法, 但是UITextField没有这个功能,要实现就得手动addTarget,其实controlevent里还有很 ...

  8. nginx 支持pathinfo的配置文件

    location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; set ...

  9. c++实现加密和解密算法以及JNI技术的应用实例

    #include "jiami.h" #include "jni.h" #include "com_test_start_CommonClassLoa ...

  10. javascript判断上传文件大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...