| 地址:https://leetcode-cn.com/problems/zigzag-conversion/ 题解:https://leetcode-cn.com/problems/zigzag-conversion/solution/hua-jie-suan-fa-6-z-zi-xing-bian-huan-by-guanpengc/ 思路:整体的思路是遍历字符串,遍历过程中将每行都看成新的字符串构成字符串数组,最后再将该数组拼接起来即可如果 numRows=1 则说明当前字符串即为结果,直接返回
 否则整个字符串需要经历,向下向右,向下向右,这样的反复循环过程,设定 down变量表示是否向下,loc 变量表示当前字符串数组的下标
 如果 down 为 true,则 loc+=1,字符串数组下标向后移动,将当前字符加入当前字符串中
 如果 down 为 false,则表示向右,则loc?=1,字符串数组下标向前移动,将当前字符加入当前字符串中
 时间复杂度:O(n),n为字符串s的长度。
 代码:	if(numRows==1) return s;//假如只有一行直接返回s
		//否则生成numRows段字符数组,最后的时候拼接在一起
		String[] t=new String[numRows];
		for(int i=0;i<numRows;i++) {
			t[i]="";
		}
		boolean down=false;//初始化转向指针
		String convert="";//最后得到的字符串
		int rows=0;//第几行字符数组
		for(int cur=0;cur<s.length();cur++) //cur是原字符串的下标
		{
		t[rows]+=s.substring(cur,cur+1);
		if(rows==0||rows==numRows-1) {
			down=!down;
		}
		if(down) rows+=1;
		else rows-=1;
    }
		for(int i=0;i<numRows;i++) {
			convert+=t[i];
			}
		return convert;
	}
 |