美文网首页程序员
6. ZigZag Conversion - LeetCode

6. ZigZag Conversion - LeetCode

作者: 037e3257fa3b | 来源:发表于2017-08-23 18:05 被阅读0次

    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
        }
    

    相关文章

      网友评论

        本文标题:6. ZigZag Conversion - LeetCode

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