Java 锯齿形变换
问题是:字符串“PAYPALISHIRING”在给定数量的行上以之字形书写,如下所示:(您可能希望以固定字体显示此模式以提高可读性) 然后逐行阅读:“Pahnaplsigigyir” 我已经写了下面的代码,看起来它工作得很好,但我可能会错过一些角落案例。你能帮我在我的答案上找到这个问题的所有角落案例吗Java 锯齿形变换,java,data-structures,Java,Data Structures,问题是:字符串“PAYPALISHIRING”在给定数量的行上以之字形书写,如下所示:(您可能希望以固定字体显示此模式以提高可读性) 然后逐行阅读:“Pahnaplsigigyir” 我已经写了下面的代码,看起来它工作得很好,但我可能会错过一些角落案例。你能帮我在我的答案上找到这个问题的所有角落案例吗 public static String zigZagConversion(String s , int rowNum){ if (s == null){ throw n
public static String zigZagConversion(String s , int rowNum){
if (s == null){
throw new IllegalArgumentException();
}
if (rowNum == 1){
return s;
}
StringBuilder str = new StringBuilder();
int step = 2 * rowNum - 2 ;
for (int i = 0 ; i < rowNum ; i++){
if( i == 0 || i == rowNum -1){
for (int j = i ; j < s.length() ; j +=step){
str.append(s.charAt(j));
}
}
else{
int step2 = 2* (rowNum - i - 1);
int step3 = step - step2;
int k = i;
boolean flag = true;
while (k < s.length()){
str.append(s.charAt(k));
if(flag){
k += step2;
flag = false;
}
else{
k +=step3;
flag = false;
}
}
}
}
return str.toString();
}
公共静态字符串之字形转换(字符串s,int rowNum){
如果(s==null){
抛出新的IllegalArgumentException();
}
如果(rowNum==1){
返回s;
}
StringBuilder str=新的StringBuilder();
int step=2*rowNum-2;
for(int i=0;i它为“PayPalishering”4提供了不正确的输出
P I N
A L S I G
Y A H R
P I
因此正确答案应该是PINALSIGYAHRPI
但您的程序会给出Pinaligyairnpi
:
少了一个“S”,多了一个“I”和一个“N”
您的修订版仍然不正确,它给出了PINALSIIGYAHNPI
问题是在<>代码中,而环在中间。
您需要交替进行步数计算,
设置标志
打开和关闭。
你的错误是只点燃了一次,再也没有回来
str.append(s.charAt(k));
if (flag) {
k += step2;
flag = false;
} else {
k += step3;
flag = true;
}
有了这个更正,我相信你的解决方案是正确的。(我还添加了一个小的改进,从if-else分支中提取了常见的str.append(s.charAt(k));
。Javascript中leetcode的曲折转换
解决方案
输出
跑
使用HashMap
public String convert(String s, int numRows) {
if (numRows == 1){
return s;
}
StringBuilder result = new StringBuilder();
Map<Integer, StringBuilder> map = new HashMap<>();
for (int i = 0; i < numRows; i++) {
map.put(i,new StringBuilder());
}
int it = 0;
boolean flip = true;
for (int i = 0; i < s.length(); i++) {
if (flip) {
if(it<s.length()){
map.get(it).append(s.charAt(i));
it++;
}
} else {
map.get(it).append(s.charAt(i));
it--;
}
if (it + 1 == numRows || it == 0)
flip = !flip;
}
for (Map.Entry entry: map.entrySet()) {
result.append(entry.getValue());
}
return result.toString();
}
公共字符串转换(字符串s,整数){
如果(numRows==1){
返回s;
}
StringBuilder结果=新建StringBuilder();
Map Map=newhashmap();
对于(int i=0;i 如果(它我在leetcode论坛上的解决方案:
“之字形”的数学算法为:
- originalDiff=numRows*2-2
- 如果->“currRow”等于第一行或最后一行
- 使用originalDiff(numRows*2-2)
- 其他->
- 对于每一条新线路:
- upperDiff+=2
- lowerDiff-=2
示例:
numRows =2 -> originalDiff = 2
PYAIHRN
APLSIIG
3 -> 4
P A H N
A P L S I I G
Y I R
numRows = 4 -> originalDiff = 6
P I N
A L S I G
Y A H R
P I
numRows = 5 -> originalDiff = 8
P H
A SI
Y I R
P L I G
A N
*/
我的解决方案:
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1) {
return s;
}
String newString = "";
int originalDiff = numRows * 2 - 2;
int diff = originalDiff;
int upperDiff = 0;
boolean isGoingDown = true;
int currIndex = 0;
int currRow = 0;
int startingIndex = 0;
for(int i = 0; i < s.length(); i++) {
System.out.println(currIndex);
newString += s.charAt(currIndex);
if(currRow == 0 || currRow == numRows - 1) {
currIndex += originalDiff;
} else {
if(isGoingDown) {
currIndex += diff;
isGoingDown = !isGoingDown;
} else {
currIndex += upperDiff;
isGoingDown = !isGoingDown;
}
}
if(currIndex >= s.length()) {
currRow++;
diff -= 2;
upperDiff += 2;
currIndex = currRow;
isGoingDown = true;
}
if(currRow == numRows) {
i = s.length();
}
}
return newString;
}
}
类解决方案{
公共字符串转换(字符串s,整数numRows){
如果(numRows==1){
返回s;
}
字符串newString=“”;
int originalDiff=numRows*2-2;
int diff=原始diff;
int-upperDiff=0;
布尔值isGoingDown=true;
int currIndex=0;
int currRow=0;
int startingIndex=0;
对于(int i=0;i=s.length()){
currRow++;
diff-=2;
upperDiff+=2;
currIndex=currRow;
isGoingDown=真;
}
如果(currRow==numRows){
i=s.长度();
}
}
返回新闻字符串;
}
}
我的解决方案是以与问题中所述相同的方式遍历字符串,最好使字符串数组的大小为numrows,其余的是按逻辑中的方式存储字符串
你可以保留索引,当索引为0时,即开始时,我们必须一直到行的末尾,然后除了第一行和最后一行之外,每个数组都有对角元素
因此,在遍历到最后一行之后,分配index=numrows-2并保存在相应的数组字符串中,减少并执行相同的操作,直到index>0,然后再次遍历到最后一行,这样做,当我们到达字符串的末尾时,就从循环中中断
然后将字符串数组中的所有字符串合并到一个新的res字符串中
class Solution {
public String convert(String s, int n) {
if(n==1 || n>=s.length())
return s;
String[] a = new String[n]; //string array
int ind=0; // index for the string array
boolean flag=true;
int cnt=0; //to keep the counter till where we have traversed the string
while(true && flag)
{
if(ind==0)
{
for(int i=0;i<n;i++)
{
a[i] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
} // here it has reached the end so we assign here
ind = n-2;
}
else if(ind>0 && ind<n && flag)
{
a[ind] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
ind--; // to move diagonally up
}
}
String res = new String("");
for(int i=0;i<a.length;i++)
{
// System.out.println(a[i].substring(4));
res += a[i].substring(4);
}
return res;
}
}
类解决方案{
公共字符串转换(字符串s,整数n){
如果(n==1 | | n>=s.length())
返回s;
String[]a=新字符串[n];//字符串数组
int ind=0;//字符串数组的索引
布尔标志=真;
int cnt=0;//将计数器保留到遍历字符串的位置
while(true&&flag)
{
如果(ind==0)
{
对于(inti=0;i0&&ind,以下是简单的解决方案
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows <= 1:
return s
res = ""
p = numRows * 2 - 2
temp = p
for i in range(0,numRows):
index = i
flag = 0
while index < len(s):
res = res + s[index]
if i == 0 or i == numRows-1:
index = index + p
else:
if flag == 0:
index = index + temp
flag = 1
else:
index = index + p-temp
flag = 0
temp = temp - 2
return res
类解决方案:
def转换(self,s:str,numRows:int)->str:
如果您正在寻找numRows。@JohnBollinger如果它完全正常工作,或者至少大部分正常工作,并且通过了大多数测试用例,那么对代码复查SE来说是很好的。事实并非如此(现在)。正确的版本(例如,使用我下面的答案)在代码上确实是很好的
numRows =2 -> originalDiff = 2
PYAIHRN
APLSIIG
3 -> 4
P A H N
A P L S I I G
Y I R
numRows = 4 -> originalDiff = 6
P I N
A L S I G
Y A H R
P I
numRows = 5 -> originalDiff = 8
P H
A SI
Y I R
P L I G
A N
*/
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1) {
return s;
}
String newString = "";
int originalDiff = numRows * 2 - 2;
int diff = originalDiff;
int upperDiff = 0;
boolean isGoingDown = true;
int currIndex = 0;
int currRow = 0;
int startingIndex = 0;
for(int i = 0; i < s.length(); i++) {
System.out.println(currIndex);
newString += s.charAt(currIndex);
if(currRow == 0 || currRow == numRows - 1) {
currIndex += originalDiff;
} else {
if(isGoingDown) {
currIndex += diff;
isGoingDown = !isGoingDown;
} else {
currIndex += upperDiff;
isGoingDown = !isGoingDown;
}
}
if(currIndex >= s.length()) {
currRow++;
diff -= 2;
upperDiff += 2;
currIndex = currRow;
isGoingDown = true;
}
if(currRow == numRows) {
i = s.length();
}
}
return newString;
}
}
class Solution {
public String convert(String s, int n) {
if(n==1 || n>=s.length())
return s;
String[] a = new String[n]; //string array
int ind=0; // index for the string array
boolean flag=true;
int cnt=0; //to keep the counter till where we have traversed the string
while(true && flag)
{
if(ind==0)
{
for(int i=0;i<n;i++)
{
a[i] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
} // here it has reached the end so we assign here
ind = n-2;
}
else if(ind>0 && ind<n && flag)
{
a[ind] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
ind--; // to move diagonally up
}
}
String res = new String("");
for(int i=0;i<a.length;i++)
{
// System.out.println(a[i].substring(4));
res += a[i].substring(4);
}
return res;
}
}
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows <= 1:
return s
res = ""
p = numRows * 2 - 2
temp = p
for i in range(0,numRows):
index = i
flag = 0
while index < len(s):
res = res + s[index]
if i == 0 or i == numRows-1:
index = index + p
else:
if flag == 0:
index = index + temp
flag = 1
else:
index = index + p-temp
flag = 0
temp = temp - 2
return res