当前位置:网站首页>Leetcode medium question sharing (5)
Leetcode medium question sharing (5)
2022-07-02 00:00:00 【PigeonEssence】
Z Font conversion
subject :

Encountered this kind of problem has horizontal and vertical , I first thought of the solution of two-dimensional array :
be supposed to Z The font , In fact, it's more like a repeating cycle on one , So we are thinking about how many characters are needed for each cycle :
hypothesis rowNum=3, So in the middle 1 individual , All in all 4 individual
hypothesis rowNum=4, So in the middle 2 individual , All in all 6 individual
hypothesis rowNum=5, So in the middle 3 individual , All in all 8 individual
So obviously , All you need is 2*rowNum-2 individual .

We can see this up and down as a cycle , We consider the space occupation of a loop in a two-dimensional array
Let's assume that the length of this array is length:
int length = s.length();
numRows A row :
int numOfRec = 2*numRows-2;
The occupation of vertical columns can be considered from numRows Descending to 0 One of the forLoop, In general, we mainly consider the number of cycles in the vertical column :
int numCols = (length+numOfRec-1)/numOfRec*(numRows-1);
Then all we need to do is traverse and add a two-dimensional array :
// Fill in below first numRows Characters , Then fill in from the bottom left to the top right
public static String convert(String s, int numRows) {
int length = s.length();
if (numRows==1 || numRows>=length){
return s;
}
// The number of characters required for a cycle
int numOfRec = 2*numRows-2;
// Number of columns
int numCols = (length+numOfRec-1)/numOfRec*(numRows-1);
// Create a 2D array
char[][] cars = new char[numRows][numCols];
// Traverse and insert a two-dimensional array
for (int i = 0, x = 0, y = 0; i < length ; ++i) {
cars[x][y]=s.charAt(i);
if (i%numOfRec<numRows-1){
++x;
}else{
--x;
++y;
}
}
// Traverse the output
StringBuilder res = new StringBuilder();
for (int j = 0; j < numRows; j++) {
for (int i = 0; i < numCols; i++) {
char c = cars[j][i];
if (c!='\u0000'){
res.append(c);
}
}
}
return res.toString();
}This problem is intuitively a two-dimensional array + The use of recycling , There is some violence in solving problems . The difficulty that needs to be considered is the duality of two-dimensional arrays for The loop will cause a timeout , To this extent, it should be simplified .

Although the result passed, I was not very satisfied , Let's optimize it later
边栏推荐
- leetcode96不同的二叉搜索树
- 求逆序数的三个方法
- Is it safe to choose mobile phone for stock trading account opening in Beijing?
- 华为HMS Core携手超图为三维GIS注入新动能
- 2021 robocom world robot developer competition - semi finals of higher vocational group
- [QT] solve the problem that QT MSVC 2017 cannot compile
- [cmake] cmake configuration in QT Creator
- How to realize parallel replication in MySQL replication
- ADO. Net SqlDataAdapter object
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
猜你喜欢

Use vb Net to convert PNG pictures into icon type icon files

华为HMS Core携手超图为三维GIS注入新动能

Chapter 6 data flow modeling

Notblank and notempty

TS初次使用、ts类型

The essence of software architecture

LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电

Concurrentskiplistmap -- principle of table skipping

2021 robocom world robot developer competition - preliminary competition of undergraduate group

ARP message header format and request flow
随机推荐
记录一下大文件上传偶然成功偶然失败问题
Windows installation WSL (II)
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
深度学习 | 三个概念:Epoch, Batch, Iteration
Which securities company is the best to open a stock account? Is there a security guarantee
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
UDS bootloader of s32kxxx bootloader
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
下载在线视频 m3u8使用教程
一个实习生的CnosDB之旅
[leetcode] length of the last word [58]
How excel opens CSV files with more than one million lines
Algolia's search needs are almost closed
Openwrt enable kV roaming
【模板】自适应辛普森积分
Deep learning | three concepts: epoch, batch, iteration
门级建模—课后习题
Correlation - intra group correlation coefficient
mysql之B tree 以及 B+tree
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电