Back home

ARTES #033

ARTES #033

ARTES 033

Algoritmo

6. Conversión ZigZag

Dificultad: Media

La cadena "PAYPALISHIRING" está escrita en un patrón de zigzag en un número determinado de filas como este: (es posible que desee mostrar este patrón en una fuente fija para una mejor legibilidad)

P   A   H   N
A P L S I I G
Y   I   R

Y luego lea línea por línea: "PAHNAPLSIIGYIR"

Escribe el código que tomará una cadena y realizará esta conversión dada una cantidad de filas:

string convert(string s, int numRows);

Ejemplo 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Ejemplo 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I

Solución

我觉得这道题考察的就是找规律,你找到规律了就能解决了.

Idioma: Rápido


class Solution{
    func convert(_ s: String, _ numRows: Int) -> String {
        if s.count <= numRows ||  numRows == 1{
            return s;
        }
        
        var rowStrs = Array(repeating: "", count: numRows)
        
        var step = 0
        var start = 0
        
        for i in 0..<s.count {
            let char = s[s.index(s.startIndex, offsetBy: i)]
            rowStrs[start].append(char)
            if start == 0 {
                step = 1;
            }
            else if start == (numRows - 1) {
                step = -1;
            }
            start = start + step
        }
        
        var result = ""
        for i in 0..<rowStrs.count  {
            result = result + rowStrs[i]
        }
        return result
    }
}

7. Entero inverso

Dificultad: Fácil

Dado un entero de 32 bits con signo, invierta los dígitos de un entero.

Ejemplo 1:

Input: 123
Output: 321

Ejemplo 2:

Input: -123
Output: -321

Ejemplo 3:

Input: 120
Output: 21

Nota:
Supongamos que estamos tratando con un entorno que solo puede almacenar números enteros dentro del rango de enteros con signo de 32 bits: [−2<sup>31</sup>,  2<sup>31 </sup>− 1]. A los efectos de este problema, suponga que su función devuelve 0 cuando el entero invertido se desborda.

Solución

Idioma: Rápido

class Solution {
    func reverse(_ x: Int) -> Int {
        var result = 0
        var tem = x
        
        while tem != 0 {
            if result > Int32.max/10 || result < Int32.min / 10 {
                return 0
            }
            
            result = result * 10 + tem % 10
            tem = tem / 10
        }
        
        return result;
    }
}

或者


class Solution {
    func reverse(_ x: Int) -> Int {
        var result = 0
        var tem = x
        
        while tem != 0 {
            
            
            result = result * 10 + tem % 10
            tem = tem / 10
            
            if result > Int32.max || result < Int32.min {
                return 0
            }
        }
        
        return result;
    }
}

Revisión

这篇文章讲的是Singleton puro设计模式和Semi-singleton设计模式:

https://dandan2009.github.io/2019/10/24/design-patterns-by-tutorials-the-power-of-OOP-part-2/

Consejos

talbeView cell上有视图正在动画,这时如果调用reloadData方法,动画会被打断,有没有方法被打断? 目前还没找到方法

Compartir

这两天把Mac系统升级到了10.15,xcode升级到了xcod e11.1,但是遇到了一些问题,在电脑不联网的情况模拟器不能安装A PP或假死,然后Charles不联网的情况下会启动很慢,如果你的电脑是开发主力,建议谨慎升级。不知道是我的环境问题还是新系统问题。