In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to achieve font conversion string in C++". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve font conversion string in C++" can help you solve the problem.
ZigZag Conversion font conversion string
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 s, int numRows)
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
After reading this question for a long time, I didn't understand how it was changed. After looking up some information on the Internet, I finally figured it out. It is necessary to put the string into a zigzag shape. For example, there is a string "0123456789ABCDEF", which is converted to zigzag as follows:
When n = 2:
0 2 4 6 8 A C E
1 3 5 7 9 B D F
When n = 3:
0 4 8 C
1 3 5 7 9 B D F
2 6 An E
When n = 4:
0 6 C
1 5 7 B D
2 4 8 An E
3 9 F
It can be found that, except for the first line and the last line, there are no zigzag numbers in the middle, and the difference between the index of the two adjacent elements in the first two lines is related to the number of lines, which is 2*nRows-2. According to this characteristic, you can find all the black elements in the position of the meta string in order and add them to the new string in order. There is also a pattern for where red elements appear (colors may not display properly on Github, see posts in the blog garden). The position of each red element is j + 2 x numRows-2-2 x I, where j is the index,i of the previous black element is the current number of lines. For example, when the red 5 in n = 4, its position is 1 + 2 x 4-2-2 x 1 = 5, which is the correct position of the original string. Knowing the correct algorithm for the location of all black and red elements, you can add them all to the new string in one go. The code is as follows:
Solution 1:
Class Solution {public: string convert (string s, int numRows) {if (numRows)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.