美文网首页
6.ZigZag Conversion(Easy)

6.ZigZag Conversion(Easy)

作者: 兰缘小妖 | 来源:发表于2016-11-01 23:15 被阅读16次

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)


And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)

should return "PAHNAPLSIIGYIR".

  题意并不难了理解,就是对一个字符串进行一个重新排列按新的规则输出

My Solution

(Java) Version 1 Time: 71ms:

  确实是慢,我对这类找规律的题目的解法大体都是找到一个类似的规律,然后用if语句来补全

public class Solution {
    public String convert(String s, int numRows) {
        int length=s.length();
        if(numRows==1||length<=numRows)return s;
        int itemCount=2*numRows-2;
        System.out.println("itemCount="+itemCount);
        int count=length%itemCount==0?length/itemCount:length/itemCount+1;
        System.out.println("count="+count);
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<numRows;i++){
            for(int j=1;j<=count;j++){
                if(i==0)sb.append(s.charAt((j-1)*itemCount));
                else if(i==numRows-1)
                    if((j*itemCount-(numRows-2))-1<length){
                        sb.append(s.charAt((j*itemCount-(numRows-2))-1));
                    }
                    else{
                        continue;
                    }
                else {
                    if(((j-1)*itemCount+i)<length)sb.append(s.charAt((j-1)*itemCount+i));
                    if((itemCount*j-i)<length)sb.append(s.charAt(itemCount*j-i));
                }
            }
        }
        return sb.toString();
    }
}

(Java) Version 2 Time: ms (By medi):

  比我慢,纯粹是记一下不同的做法

public class Solution {
    public String convert(String s, int numRows) {
        String res="";
        List<String> strow = new ArrayList<>();
        for(int i=0; i<numRows; i++){
            strow.add(0, "");
        } 
        int row=0;
        boolean turn=false;
        for(int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            String news = strow.get(row)+String.valueOf(c);
            strow.remove(row);
            strow.add(row,news);
            if(!turn)
            row++;
            else{
            row--;
            }   
            row=row%numRows;
        
            if(row==0 && !turn){
            row=numRows-2;
            if(row<0)
                row=0;
            turn^= true;
            }
        
            if(row==0 && turn){
            row=0;
            turn^= true;
            }
        }
    
    for(String st: strow){
        res=res+st;
    }
    
    return res;
    }
}

相关文章

  • 6.ZigZag Conversion(Easy)

    The string "PAYPALISHIRING" is written in a zigzag patter...

  • 6.ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag patter...

  • 6.ZigZag Conversion

    题目描述 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: 之后从左往右,逐行读取字符:"P...

  • 6.ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag patter...

  • 6.ZigZag Conversion

    将给定的一个字符串按照Z字形排放,然后按照行输出结果字符串 题目 : 思路: 将化成的Z字形的每一行,用一个字符串...

  • Leetcode 6.ZigZag Conversion

    题目 题目大意:给定一个字符串和一个数字,将字符串排列成“S”形,数字代表“S”形型高度,最后返回将每一横排相加的...

  • leetcode - 6.ZigZag Conversion

    Description The string "PAYPALISHIRING" is written in a z...

  • ZigZag Conversion

    每日算法——letcode系列 问题 ZigZag Conversion Difficulty: Easy The...

  • 6.ZigZag Conversion(ZigZag转换)

    字符串“PAYPALISHIRING”以给定行数的锯齿形图案写成:(您可能希望以固定字体显示此模式以获得更好的可读...

  • 每天一题LeetCode【第1天】

    T6. ZigZag Conversion 【Easy】 题目 把字符串 "PAYPALISHIRING" 用Zi...

网友评论

      本文标题:6.ZigZag Conversion(Easy)

      本文链接:https://www.haomeiwen.com/subject/esqvuttx.html