当前位置:网站首页>Leetcode 6[finding rules] Z-transform the leetcode path of heroding
Leetcode 6[finding rules] Z-transform the leetcode path of heroding
2022-06-12 03:01:00 【HERODING23】

Their thinking
A problem that can be solved by observing the law carefully , In fact, the string is not in accordance with N, But according to V Composed of , in other words , The minimum period is V type ,T by 2*numRows-2, In this way, the characters of each line can be found according to the periodic law , Further observation will reveal , For each V, Draw a horizontal line from top to bottom , There is only one dot except the first and last lines , The other positions are two points and symmetrical , So when we traverse , As long as it does not exceed the length of the string , For non first and last lines , And find out the symmetrical characters . The notes are already very detailed , The code is as follows :
Code
class Solution {
public:
string convert(string s, int numRows) {
int n = s.size();
// If it happens to be a row or a column
if(numRows == 1 || n <= numRows) {
return s;
}
// A cycle
int T = 2 * numRows - 2;
string ans;
// Go through every line
for(int i = 0; i < numRows; i ++) {
// Traverse each cycle i Characters on line
for(int j = 0; j + i < n; j += T) {
ans += s[i + j];
// Adjacent characters
if(i > 0 && i < numRows - 1 && j + T - i < n) {
ans += s[j + T - i];
}
}
}
return ans;
}
};
边栏推荐
- Audio and video technology under the trend of full true Internet | Q recommendation
- Intel case
- [high code file format API] downing provides you with the file format API set Aspose, which can create, convert and operate more than 100 file formats in just a few lines of code
- Interpreting Julia's 2021: step by step towards the mainstream programming language
- min25筛
- errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
- Wechat applet project example - Fitness calculator
- errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306; Postman error
- (9) Serial port interrupt
- SSH public key login failed with error: Sign_ and_ send_ pubkey: no mutual signature supported
猜你喜欢

Intel case

Infinite loop judgment method;

微信小程序項目實例——體質計算器

【点云压缩】Variational Image Compression with A Scale Hyperprior

Hypergraph tilted data is merged into root node and transferred to 3dfiles

Kubernetes affinity learning notes

How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?

Laravel 8 selects JWT for interface verification

Unity3D中DrawCall、Batches、SetPassCall

$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
随机推荐
oracle之模式对象
(9) Serial port interrupt
Wechat applet project example - Fitness calculator
Audio and video technology under the trend of full true Internet | Q recommendation
Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model
Drawcall, batches, setpasscall in unity3d
cupp字典生成工具(同类工具还有crunch)
Computer common sense
Apache simple honeypot
Sparse tensor based point cloud attribute compression
PHP life cycle and swoole life cycle
oracle之序列
maya前台渲染插件mel脚本工具
微积分复习2
跨域有哪些解决方法?
Function templatesfunction templates
$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
oracle之用户和表空间
Comparaison de la taille des fractions
Selection (046) - what is the output of the following code?