6. Zigzag Conversion

2023-12-09 17:52
文章标签 conversion zigzag

本文主要是介绍6. Zigzag Conversion,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 按照下标找规律
  • 注意leetcode的运行输出,如果其中一组用例出现死循环,输出结果会在一个文件,即部分测试用例正确,部分错误且出现死循环,则需辨别输出结果属于哪一份测试用例
class Solution {
public:string convert(string s, int numRows) {int len = s.size();int d = 2 * numRows - 2;if(d == 0) return s;string ret;int sd = d;for(int i = 0; i < numRows; i++){int tmp = i;  while(tmp < len && sd > 0){ret += s[tmp];if(sd != d && tmp + sd < len)ret += s[tmp + sd];tmp += d;}  sd -= 2;}int j = numRows - 1;while(j < len){ret += s[j];j += d;}return ret;}
};

这篇关于6. Zigzag Conversion的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/474579

相关文章

Retrieval-based-Voice-Conversion-WebUI模型构建指南

一、模型介绍 Retrieval-based-Voice-Conversion-WebUI(简称 RVC)模型是一个基于 VITS(Variational Inference with adversarial learning for end-to-end Text-to-Speech)的简单易用的语音转换框架。 具有以下特点 简单易用:RVC 模型通过简单易用的网页界面,使得用户无需深入了

x264 编码器 AArch64汇编系列:zigzag 扫描相关汇编函数

zigzag 在x264_zigzag_init函数中初始化具体的 zigzag 实现函数: 以scan_4x4为例 c 语言实现 4x4 变换块扫描:zigzag_scan_4x4_frame。 #define ZIGZAG4_FRAME\ZIGDC( 0,

TensorFlow测试程序报异常:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np

使用安装好的tensorflow-gpu 进行程序测试时出现异常: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(

第六题:Z字形变换(Zigzag Conversion)

题目描述: 将给定的字符串 s 以指定的行数 numRows 进行“Z字形”排列,然后按行读出字符串并返回。 示例: 输入:s = "PAYPALISHIRING", numRows = 3 输出:"PAHNAPLSIIGYIR" 输入:s = "PAYPALISHIRING", numRows = 4 输出:"PINALSIGYAHRPI" 要求: 你需要将字符串 s 按照 num

LeetCode | Binary Tree Zigzag Level Order Traversal

二叉树Z型输出 Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given

ZigZag Conversion题目及解法

问题描述: 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 NA P

Binary Tree Zigzag Level Order Traversal问题及解法

问题描述: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). 示例: Given binary t

MySQL中的隐式转换(Implicit Conversion)

MySQL中的隐式转换(Implicit Conversion)指的是在SQL语句的执行过程中,数据库管理系统(DBMS)自动进行的数据类型转换。这种转换通常发生在数据类型不匹配但需要进行比较、计算或赋值等操作时。 以下是一些关于MySQL隐式转换的常见场景和注意事项: 1、字符串和数字之间的转换: 当字符串和数字进行算术运算时,字符串会被尝试转换为数字(如果可能)。例如,‘110’ + 1

Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB

在使用xcode5 sdk iOS7环境,创建图形上下文进行图形绘制,合并,裁剪,特效处理等时避免不了使用如下方法创建位图: 在 iOS7以前,是使用如下方法创建的: CG_EXTERN CGContextRef CGBitmapContextCreate(void *data, size_t width,   size_t height, size_t bitsPerComponent,

Leetcode 103 Binary Tree Zigzag Level Order Traversal(BFS)

题目连接:Leetcode 103 Binary Tree Zigzag Level Order Traversal 解题思路:与Leetcode 102 一样,使用BFS层次遍历二叉树,不同的是,对于奇数层,要翻转一下结点顺序。 /*** Definition for a binary tree node.* struct TreeNode {* int val;* Tree