6.ZigZag Conversion(Easy)

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;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,774評論 0 33
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,969評論 19 139
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,936評論 0 0
  • 寶貝!大概再有一兩天我們就要見面了,媽媽的內心好激動呢!謝謝你這么聽話,能讓我把今天的工作做完,因為今天事情真的很...
    攸妍王坤閱讀 82評論 0 0
  • 1 宇宙大爆炸0·00001秒。原子和中子誕生。、 2分46秒,反物質湮滅。十億分之一的物質留存,太初核合成,氫核...
    西西余閱讀 227評論 2 0