题目如下:

C++代码:

#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if (numRows <= || s.length() <= numRows)
return s;
string result;
int ZigSpan = * numRows - ;
for (int i = ; i < numRows; i++){
for (int j = i; j < s.length(); j += ZigSpan){
result.push_back(s[j]);
if (i != && i != numRows - ){
int t = j + ZigSpan - * i;
if (t < s.length())
result.push_back(s[t]);
}
}
}
return result;
}
};
void ZigZag(){
string s = "abcdefghijklmn";
Solution *ss = new Solution();
cout << ss->convert(s, ) << endl;
}

LeetCode(6)ZigZag Conversion的更多相关文章

  1. LeetCode(6) ZigZag Conversion

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  2. (字符串)ZigZag Conversion

    [解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...

  3. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. android sudio 执行的中文是乱码解决方式

    1.File-->Setings-->查找file encodings 例如以下图 2.将 IDE Encoding .Project Encoding.Default encoding ...

  2. ARIMA模型实例讲解——网络流量预测可以使用啊

    ARIMA模型实例讲解:时间序列预测需要多少历史数据? from:https://www.leiphone.com/news/201704/6zgOPEjmlvMpfvaB.html   雷锋网按:本 ...

  3. POJ 2114 点分治

    思路: 点分治 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> u ...

  4. 《剑指offer》二叉搜索树与双向链表

    一.题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向 二.输入描述 输入一棵二叉搜索树 三.输出描述 将该二叉搜索树转换成一个 ...

  5. 路飞学城-Python开发-第三章

    # 数据结构: # goods = [ # {"name": "电脑", "price": 1999}, # {"name&quo ...

  6. 浅说套接字socket做个小小的监控

    socket 的简介 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是"孔"或"插座".作为 ...

  7. java jar打包命令使用

    用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ... 选项: -c 创建新的存档 -t 列出存档内容的列表 -x 展开存档中的命名的 ...

  8. mysql 基础函数语句

    1:查看当前登陆用户 select user(): 2:切换数据库 use mysql; 查看该表用户 select user,host from user; 4:退出数据库 5:查看数据库版本 se ...

  9. pandas 8 画图

    from __future__ import print_function import pandas as pd import numpy as np import matplotlib.pyplo ...

  10. Qt之QImageWriter

    简述 QImageWriter类为写入图像至文件或设备提供了一个独立的接口.QImageWriter支持格式特定的选项(如:质量和压缩率),可以在存储图像之前进行设置.如果不需要这些选项,可以使用QI ...