当前位置:网站首页>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
边栏推荐
- BlocProvider为什么感觉和Provider很相似?
- Digital transformation has a long way to go, so how to take the key first step
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- 时间复杂度与空间复杂度
- [leetcode] length of the last word [58]
- Relatively easy to understand PID understanding
- Difficult to get up syndrome (bit by bit greed)
- Is there a piece of code that makes you convinced by human wisdom
- Kubernetes resource object introduction and common commands (III)
- 写给当前及未来博士研究生一些建议整理分享
猜你喜欢

深度学习 | 三个概念:Epoch, Batch, Iteration

BlocProvider为什么感觉和Provider很相似?

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

LeetCode中等题题分享(5)

【QT】對於Qt MSVC 2017無法編譯的問題解决

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

多表操作-一对一,一对多与多对多

USB-IF协会与各种接口的由来
![[QT] qtcreator uninstall and installation (abnormal state)](/img/66/1b96326d87bca2a790a6694f38e79e.png)
[QT] qtcreator uninstall and installation (abnormal state)

Write some suggestions to current and future doctoral students to sort out and share
随机推荐
门级建模—课后习题
Notblank and notempty
S32Kxxx bootloader之UDS bootloader
kubernetes资源对象介绍及常用命令(三)
Jielizhi, production line assembly link [chapter]
二叉搜索树的创建,查找,添加,删除操作
PostgreSQL notes (10) dynamically execute syntax parsing process
Graduation season is both a farewell and a new beginning
Shell process control
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
[QT] solve the problem that QT MSVC 2017 cannot compile
Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
ERP项目施行计划的目的是什么?
Pytorch learning record
golang中的iota
The difference between timer and scheduledthreadpoolexecutor
安全协议重点
algolia 搜索需求,做的快自闭了...
[leetcode] length of the last word [58]
【CMake】Qt creator 里面的 cmake 配置