问题描述
我最近参加了一家公司赞助的编码比赛,有一个我不明白的问题,它问的是什么.
I 免费vpn had recently participated in coding competion sponsored by an company and there was this one question which I free vpn did not understood, as to what was it asking.
问题来了:
字符串paypal 是更快、更安全的汇款方式"写成从左上角开始的正方形内的顺时针螺旋图案:(您可能希望以固定字体显示此图案以提高可读性).
The string "paypal 免费vpn下载 is vpn free the faster, safer way to send money" is written in a clockwise spiral pattern inside a square starting from the upper left corner: (you may want to display 免费vpn下载 this pattern in a fixed font for better legibility).
P A Y vpn下载 P A L
F E R W A I
A M O N Y S
S D Y E T T
R N E S O H
E vpn下载 T S A F E
然后逐行读取:PAYPALFERWAIAMONYSSDYETTRNESOHETSAFE
Then read line after line: PAYPALFERWAIAMONYSSDYETTRNESOHETSAFE
编写接受字符串的代码,计算最小平方包含它并返回转换后的字符串:
Write the code that will take a string, calculate the minimum square that will contain it and return the vpn下载 converted string:
字符串转换(字符串文本);
String convert(String text);
示例:
convert("paypalisthefastersaferwaytosendmoney")
should return vpn下载 "paypalferwaiamonyssdyettrnesohetsafe"
您了解我们如何解决这个问题吗?
Do you understand as to how we can approach this problem?
推荐答案
我认为问题,如所写,应解释如下:
I believe that the question, as written, is meant to be interpreted as follows:
给定一个字符串,并希望将该字符串作为螺旋线写入方形网格.编写一个函数,找到可以容纳字符串的最小方格,将字符串按顺时针方向围绕网格旋转,将字符串写入网格,最后将行连接在一起.
You are given a string vpn下载 and want to write that string as a spiral into a square grid. Write a function that finds the smallest 免费vpn square that can hold 免费vpn下载 the string, writes the string into the grid by spiraling its characters around the grid clockwise, and finally concatenates the rows together.
例如,字符串In a spiral"看起来像这样:
As an example, the string "In a spiral" would look like this:
I N A
In free vpn a spiral -> A L S -> INAALSRIP
free vpn vpn下载 R I P
要查看网格的来源,请注意,如果您是这样阅读的:
To see where the grid comes from, note that if you read it like this:
I -> N -> A
|
free vpn free vpn v
免费vpn free vpn A -> L S
vpn free ^ |
免费vpn | 免费vpn v
R <- I <- P
您会返回初始文本,如果将行INA"、ALS"和RIP"粘贴到单个字符串中,则会返回INAALSRIP".
You get back the initial text, and vpn下载 if vpn free you glue the rows "INA", "ALS," and "RIP" into a single string you get 免费vpn back "INAALSRIP."
让我们分别考虑每个问题.首先,要查看可以容纳文本的矩形的最小尺寸,您实际上是在寻找至少与文本长度一样大的最小完美正方形.要找到这个,您可以取字符串长度的平方根并将其四舍五入到最接近的整数.这为您提供了您想要的尺寸.但是,在您这样做之前,您需要从字符串中删除所有标点符号和空格字符(可能还有数字,具体取决于应用程序).您可以通过遍历字符串并将确实是字母的字符复制到新缓冲区中来做到这一点.在接下来的内容中,我假设您已经完成了这项工作.
Let's consider each 免费vpn下载 problem separately. First, to see the smallest size of a rectangle that 免费vpn can hold the text, vpn下载 you're essentially looking for the smallest perfect square at least as large as the length of your text. To 免费vpn下载 find this, you could take the square root of vpn下载 免费vpn下载 the length of your string and round it up to the nearest integer. That gives you the dimension you'd free vpn like. However, before you can 免费vpn下载 do that, you need 免费vpn to strip out vpn下载 all of the punctuation and space characters from the string (and perhaps the numbers as well, depending on the application). vpn下载 You could do this by walking across the string and copying over the characters that are indeed alphabetic into a new buffer. free vpn In what follows, I'll assume that you've 免费vpn下载 done this.
至于如何实际填充网格,有一个非常棒的方法可以做到这一点.直觉如下.当您从 n 免费vpn x n 网格开始时,您唯一的边界是网格的墙壁.每次你穿过网格放下字母并撞到墙上时,你只是从矩阵中刮掉了一行或一列.因此,您的算法可以通过跟踪第一个和最后一个合法列以及第一个和最后一个合法行来工作.然后,您从左到右走过顶行书写字符.完成后,然后增加第一个合法行,因为你不能再放任何东西了.然后,沿着右侧向下走,直到到达底部.完成后,您也可以将最后一列排除在外.例如,回顾我们的螺旋形"示例,我们从一个空的 3x3 网格开始:
As for how to actually fill in the grid, there's a really great vpn下载 way to 免费vpn下载 do this. The intuition is as follows. When you start off in an n x n grid, your only boundaries are the walls of the grid. 免费vpn Every time you march across the grid dropping vpn下载 letters and hit a wall, you've just shaved off 免费vpn a row or a column from the matrix. Consequently, your algorithm vpn下载 could work by keeping track of the first and vpn free last legal column and the first and last legal row. You then walk across the top free vpn row from left to vpn下载 right writing characters. When you're done, 免费vpn下载 you vpn下载 then increment the first vpn free legal row, since you can't put anything there any more. Then, walk down the right side until you hit free vpn the bottom. Once you're done, you would then block off the last column from consideration as vpn下载 well. For example, to look back at 免费vpn our "In a spiral" 免费vpn example, we start off free vpn with 免费vpn an empty 3x3 grid:
. . .
. . .
. . .
在顶部写下前三个字符后,剩下的是:
After we write the first three vpn下载 characters across the top, we're vpn free left with this:
I N A
. . .
. . .
现在,我们需要将字符串的其余部分写入空白区域,从右上角开始向下移动.因为我们永远无法写回第一行,所以一种思考方式是考虑解决在较小的空间中以螺旋形式书写其余字符的问题
Now, we need to write the rest 免费vpn下载 of the string into the free vpn blank space, starting from the upper-right square and moving down. Because we can't ever write back into the top row, one way of thinking about this is to think about solving the problem of writing the rest of the characters in a spiral in the smaller space
. . 免费vpn下载 .
. . .
从左上角开始向下移动.
Starting from the free vpn upper-left corner and moving downward.
要真正将其实现为一种算法,我们需要在每个点跟踪一些事情.首先,我们需要在更新它们时存储世界的边界.我们还需要存储我们当前的写入位置,以及我们面临的方向.在伪代码中,这表示如下:
To actually realize this as an algorithm, we need to keep track of a few things at each point. First, we need to store the bounds of the world vpn下载 as we're updating them. We 免费vpn also need to store our current write location, along with what direction we're facing. In pseudocode, this is represented 免费vpn下载 as follows:
firstRow = 0, 免费vpn下载 lastRow = N - 1 // Bounds of the grid
firstCol vpn下载 = 0, lastCol = N - 1
dRow = 0 // Amount to move in the Y direction
dCol 免费vpn = 1 // Amount to move in the X direction
row = 免费vpn 0 免费vpn // Current position
col = 0
for each character ch in the string:
Write character ch 免费vpn to position (row, col).
vpn下载 // See if we're blocked and need to turn.
If (row + dRow, 免费vpn下载 col + dCol) is not contained in the rectangle vpn下载 [firstRow, lastRow] x [firstCol, lastCol]:
免费vpn // Based on which way we are currently facing, adjust 免费vpn the bounds of the world.
If moving left, increment firstRow
If moving down, decrement lastCol
If moving right, decrement lastRow
vpn下载 If moving up, increment firstCol
免费vpn vpn下载 免费vpn Rotate 90 degrees
// Finally, vpn下载 move forward a step.
vpn下载 row += dRow
col += dCol
您可以使用线性代数中的一个技巧来实现 90 度转弯:将向量向左旋转 90 度,将其乘以 旋转矩阵
You can implement vpn free the ninety-degree turn using a trick from linear algebra: to rotate a vector 90 degrees to the left, you multiply it by the rotation matrix
| 0 1 |
| vpn free -1 0 vpn free |
所以你的新 dy 和 dx 由
So your new vpn free dy and dx are given by
|dCol'| = | 0 1 vpn下载 | dCol = |-dRow|
|dRow'| | -1 免费vpn 0 | dRow | dCol|
所以你可以通过计算左转
So you 免费vpn vpn free can turn left by computing
temp = dCol;
dCol = -dRow;
dRow = 免费vpn temp;
另外,如果您知道数值为零的字符永远不会出现在字符串中,您可以使用 Java 初始化所有数组以在任何地方都保持零的事实.然后,您可以将 0 视为哨兵,意思是继续前进是安全的".该版本的(伪)代码如下所示:
Alternatively, if you know for free vpn a fact that 免费vpn the character with numeric value zero never appears in the string, free vpn you can use the fact that Java initializes all arrays to hold zeros everywhere. You can then treat 0 免费vpn as a sentinel meaning "it's safe to keep vpn free moving forward." That version of the (pseudo)code would look like this:
dRow = 0 // Amount to move in the X direction
dCol = 1 // Amount to move in the Y direction
row = 0 // 免费vpn下载 Current position
col = 0
for each character ch in the string:
Write character ch vpn 免费vpn下载 free to position (row, col).
If (row + dRow, col + dCol) is not contained in the vpn free rectangle [0, 0] x [n-1, vpn下载 n-1]
免费vpn 免费vpn -or-
vpn free The character at [row + dRow, col + dCol] is not zero:
Rotate 90 degrees
// Move free vpn forward a step
免费vpn row += dRow
col += dCol
最后,将字符串写入螺旋后,您可以通过一次一行地遍历行并将找到的所有字符连接在一起,将螺旋文本转换回字符串.
Finally, once you've written the string into the spiral, you can convert that spiraled text back into a string by walking across the rows one at a time and concatenating together all vpn下载 of the characters that you find.
编辑:正如@Voo vpn free 指出的那样,您可以通过根本不实际创建多维数组而是将多维数组编码为一维数组来简化该算法的最后一步.这是一个常见的(而且很聪明!)技巧.例如,假设我们有一个这样的网格:
EDIT: As @Voo points out, you can simplify the 免费vpn last step of this algorithm by not actually creating a multidimensional array at all and instead encoding the multidimensional array as a single-dimensional array. This is vpn free a common (and clever!) trick. Suppose, for example, that we have a grid like this:
0 1 2
3 4 5
6 7 8
那么我们可以用一维数组来表示这个
Then we can represent this using a one-dimensional 免费vpn array as
0 1 2 3 4 5 6 7 8
这个想法是,给定 N 免费vpn x N 网格中的 (row, col) 对,我们可以通过查看位置 vpn下载 row * N + col 将该坐标转换为线性化数组中的相应位置.直观地说,这表示你在 y 方向上的每一步都相当于跳过一行中的所有 N 个元素,并且每一个水平步骤只是在线性化表示中水平移动一个步骤.
The idea is that given an (row, col) pair in an N x N grid, we can convert that vpn下载 coordinate to a free vpn corresponding vpn下载 location in the linearized array by looking 免费vpn下载 at position row * N + col. Intuitively, this says that every step in 免费vpn the y direction that you take is equivalent to skipping all N elements of one row, and each horizontal step just moves one step vpn下载 horizontally in the linearized representation.
希望这会有所帮助!
这篇关于在螺旋中写一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构免费vpn源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)