当前位置:网站首页>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
边栏推荐
- [QT] QT cannot find a solution to the compiler using msvc2017
- 一个实习生的CnosDB之旅
- Operate database transactions with jpatractionmanager
- 边缘计算概述
- [QT] qtcreator uninstall and installation (abnormal state)
- Openwrt enable kV roaming
- [QT] test whether QT can connect to the database
- Selectively inhibiting learning bias for active sampling
- Use the htaccess file to prohibit the script execution permission in the directory
- Windows installation WSL (II)
猜你喜欢
Overview of edge calculation
多表操作-一对一,一对多与多对多
.env.xxx 文件,加了常量,却undefined
leetcode96不同的二叉搜索树
【QT】對於Qt MSVC 2017無法編譯的問題解决
Why does blocprovider feel similar to provider?
Difficult to get up syndrome (bit by bit greed)
牛客-练习赛101-推理小丑
Various global files related to [.Net core] program
[QT] qtcreator uninstall and installation (abnormal state)
随机推荐
ADO.NET之SqlDataAdpter对象
【ES实战】ES上的安全性运行方式
cookie、session、tooken
Key points of security agreement
[QT] solve the problem that QT MSVC 2017 cannot compile
Windows 7 install MySQL error: 1067
Niuke - Practice 101 - reasoning clown
[C #] dependency injection and Autofac
Is it safe to choose mobile phone for stock trading account opening in Beijing?
一个实习生的CnosDB之旅
Relatively easy to understand PID understanding
Use pair to do unordered_ Key value of map
在证券账户上买基金安全吗?哪里可以买基金
【CMake】Qt creator 里面的 cmake 配置
PostgreSQL notes (10) dynamically execute syntax parsing process
股票开户哪个证券公司最好,有安全保障吗
Three methods of finding inverse numbers
PostgreSQL source code (57) why is the performance gap so large in hot update?
Material design component - use bottomsheet to show extended content (I)
vue 强制清理浏览器缓存