6. ZigZag Conversion - LeetCode

LeetCode Problems Solutions

question description: 問題描述

//問題截圖:


origin.png

//鋸齒狀


sawtooth.png

Thinking
你們可以自己先考慮一下。

solution with java - Java解決方案

public String convert(String s, int numRows) {


        if (numRows == 1) { //特殊處理,建議單獨寫在這里,避免后面邏輯復雜
            return s;
        }

        String resultStr = ""; // 結果值


        int keyIndex = 0; //字典的key值,只會是從 0 到 numrows - 1 的值
        boolean israsing = true; // 是否遞增 01234321012343210


        Map<String,String> dict = new HashMap<>();//將傳進來的字符串 有規律的保存進字典

        char[] characters = s.toCharArray();
        for (int i = 0 ; i < characters.length ; i ++){

            Character chars = characters[i];

            //確定在第幾行
            if (keyIndex == 0) {

                israsing = true;

            }else if (keyIndex == (numRows - 1)) {

                israsing = false;
            }


            String key = keyIndex + "";
            if (dict.get(key) == null) {
                dict.put(key,chars.toString());
            }else{
                dict.put(key,dict.get(key) + chars.toString());
            }



            if (israsing) {
                keyIndex += 1;
            }else{
                keyIndex -= 1;
            }
        }


        for (int j = 0 ; j < numRows ; j ++){
            String index = j + "";
            if (dict.get(index) != null){
                resultStr += dict.get(index);
            }
        }

        return resultStr;

    }

solution with swift - swift解決方案

func convert(_ s: String, _ numRows: Int) -> String {
        
        
        if numRows == 1 { //特殊處理,建議單獨寫在這里,避免后面邏輯復雜
            return s
        }
        
        var resultStr = "" // 結果值


        var keyIndex = 0 //字典的key值,只會是從 0 到 numrows - 1 的值
        var israsing = true // 是否遞增 01234321012343210
        
        var dict = [String:String]() //將傳進來的字符串 有規律的保存進字典
        
        for char in s.characters{
            
//            let startIndex = s.index(s.startIndex, offsetBy: multiple * numRows + remainder)
//            let endIndex = s.index(s.startIndex, offsetBy: index + 1)
//            
//            resultStr = s.replacingCharacters(in: startIndex..<endIndex, with: "\(char)")
            
            //確定在第幾行
            if keyIndex == 0 {
                
                israsing = true
                
            }else if keyIndex == (numRows - 1) {
                
                israsing = false
            }
            
            
            let key = String(keyIndex)
            if (dict[key] == nil) {
                dict[key] = "\(char)"
            }else{
                dict[key] = dict[key]! + "\(char)"
            }
            
            
           
            if israsing {
                keyIndex += 1
            }else{
                keyIndex -= 1
            }
        }
        
        
        for row in 0..<numRows{
            
            let index = String(row)
            
            if dict[index] != nil {
                resultStr += dict[index]!
            }
            
        }
        
        
        return resultStr
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容