重拾编程之路--jeetcode(java)--ZigZag Conversion

解题思路:

1)首行和末行的输出按行序+固定间隔(num_row=2*numRows-2)输出

2)中间行从第二行往下每行的固定间隔成对出现为(temp-2,num_row-temp-2);

    temp=num_row;

 (temp-2,num_row-temp-2);

   temp=temp-2;

  判断(当前数组索引+num_row<字符串长度)

         成立则按固定间隔(temp-2,num_row-temp-2)输出两个字符串;更新当前索引;

        不成立则判断(当前索引值+第一个固定间隔<字符串长度)

                成立输出对应的字符串,并退出当前循环;

                否则直接退出当前循环

               

package com.lulu.leetcode;


public class Solution {


public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}


if (s == null) {
return null;
}
int len = s.length();
String string = new String();
int num_row = 2 * numRows - 2;
int temp = num_row;
for (int i = 0; i < numRows; i++) {
System.out.println(i);


if (i == 0 || i == numRows - 1) {
System.out.println("if");
int j = i;
// 首行或末行输出
while (j < len) {
System.out.print(j);
string = string.concat(s.charAt(j) + "");
j = j + num_row;
}


System.out.println(string);
}


// 中间行输出


else {
int j = temp - 2;
int k = temp - j;
System.out.println("j=" + j + "k=" + k);
if (j > 0) {
int index = i;
string = string.concat(s.charAt(index) + "");
while (index < len) {
if (index + num_row < len) {
string = string.concat(s.charAt(index + j) + "");
string = string.concat(s.charAt(index + num_row)
+ "");
index = index + num_row;
System.out.println("index=" + index);
System.out.println(string);
} else {
if (index + j < len) {
string = string
.concat(s.charAt(index + j) + "");
break;
} else
break;
}
}
temp = temp - 2;
}
System.out.println(string);
}
}
return string;


}


public static void main(String args[]) {
Solution solution = new Solution();
System.out.print(solution.convert("abcde", 4));
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值