6. ZigZag Conversion [easy] (Python)

本文介绍了LeetCode上的ZigZag Conversion问题,解释了如何将字符串按ZigZag模式转换,并提供了两种Python解决方案:模拟过程和数学分析。通过示例展示了如何将'PAYPALISHIRING'转换为'PAHNAPLSIIGYIR'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

https://leetcode.com/problems/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   N
A P L S I I G
Y   I   R

And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.

题目翻译

zigzag字符串转换,具体的规则见题目原文,太长不翻译了。。。
要写的函数接收两个参数,要转换的字符串text和zigzag的行数nRows,返回转换后的字符串。

思路方法

思路一

模拟书写zigzag字符串的过程。定义一个有numRows个元素的数组,每个元素初始是空字符串,代表numRows个行字符串的初始情况;然后扫描输入字符串s,依次将每个字符添加到相应行字符串的末尾;最后将所有行字符串拼接即得结果。
注意,代码中用了取模操作来判断:是否到了需要换一个方向书写zigzag字符的时候。

代码

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows <= 1 or numRows >= len(s):
            return s
        arr = [''] * numRows
        line, step = 0, -1
        for c in s:
            arr[line] += c
            if line % (numRows-1) == 0:
                step = - step
            line += step
        return ''.join(arr)

思路二

如果稍微考虑的数学一点,那么s中的第i个字符(下标从第0个开始),如果按照zigzag书写方式会出现在的行数为(行数为0到numRows-1行):
i % (2 * numRows - 2), if i % (2 * numRows - 2) < numRows
2 * numRows - 2 - (i % (2 * numRows - 2)), if i % (2 * numRows - 2) >= numRows
有了这个结果,对于任意一个位置的字符我们都知道它应该在第几行。下面的代码仍然是顺序扫描原字符串s,当然也可以有别的办法。

代码

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows <= 1 or numRows >= len(s):
            return s
        arr = [''] * numRows
        for i in xrange(len(s)):
            tmp = i % (numRows + numRows - 2)
            if tmp < numRows:
                arr[tmp] += s[i]
            else:
                arr[numRows + numRows - 2 - tmp] += s[i]
        return ''.join(arr)

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.youkuaiyun.com/coder_orz/article/details/52039689

<think>好的,用户想了解TradingView中的MTF.ZigZag指标的用法和实现。首先,我需要确认MTF在这里指的是多时间框架(Multi-TimeFrame),而ZigZag指标通常用于识别价格波动的转折点。用户可能是在寻找如何在不同时间周期内应用ZigZag指标,或者如何编写相关的PineScript代码。首先,我应该解释MTF.ZigZag的基本概念,说明它结合了多时间框架分析和ZigZag的特性。然后,可能需要提供示例代码,展示如何在TradingView的Pine Script中实现这一指标。要注意代码的正确性,比如使用security函数获取不同时间框架的数据,并正确处理可能出现的偏移问题。 接下来,用户可能关心实际应用中的问题,比如参数设置、不同时间框架的协调,或者与其他指标的结合使用。因此,生成相关问题时要覆盖这些方面,比如参数优化、信号确认方法、多时间框架的冲突解决等。同时,需要确保回答符合用户的要求,使用中文,正确应用LaTeX格式,行内公式用$...$,独立公式用$$...$$。例如,提到价格波动幅度时可以用$threshold$表示阈值。最后,检查是否有遗漏点,比如MTF.ZigZag的常见错误或调试方法,确保回答全面且实用。</think>### TradingView中MTF.ZigZag指标的用法与实现 #### 一、MTF.ZigZag指标基础 MTF(Multi-TimeFrame)ZigZag指标通过结合多时间框架分析和ZigZag形态识别,帮助交易者捕捉关键价格转折点。其核心逻辑是: $$ \Delta P = |P_t - P_{t-n}| \geq threshold $$ 当价格波动幅度超过设定阈值时绘制线段,并在不同时间框架间同步信号[^1]。 #### 二、Pine Script实现示例 ```pine //@version=5 indicator("MTF ZigZag", overlay=true) input_timeframe = input.timeframe("D") // 选择更高时间框架 input_deviation = input.float(1.0) // 波动阈值(百分比) // 获取多时间框架数据 high_tf = request.security(syminfo.tickerid, input_timeframe, high) low_tf = request.security(syminfo.tickerid, input_timeframe, low) // ZigZag核心逻辑 var zigzag = array.new_float(0) if ta.change(high_tf) * ta.change(low_tf) < 0 array.push(zigzag, na(array.size(zigzag)) ? high_tf : array.get(zigzag, array.size(zigzag)-1) * (1 + input_deviation/100)) plot(array.size(zigzag) >= 2 ? array.get(zigzag, array.size(zigzag)-1) : na) ``` #### 三、关键参数说明 1. 时间框架选择:支持$1D$到$1M$等标准周期 2. 波动阈值:建议$0.5\%-3\%$(根据品种波动性调整) 3. 信号确认:需配合$RSI$或$MACD$等指标验证[^2] #### 四、典型应用场景 1. 多周期趋势确认(日线与4小时线共振) 2. 斐波那契扩展位测算 3. 波浪理论计数辅助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值