ARTES #033
ARTES #033
ARTES 033
Algoritmo
6. Conversão ZigZag
Dificuldade: Média
A string "PAYPALISHIRING" é escrita em um padrão ziguezague em um determinado número de linhas como este: (você pode querer exibir este padrão em uma fonte fixa para melhor legibilidade)
P A H N
A P L S I I G
Y I R
E então leia linha por linha: "PAHNAPLSIIGYIR"
Escreva o código que pegará uma string e fará essa conversão dado um número de linhas:
string convert(string s, int numRows);
Exemplo 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Exemplo 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Solução
我觉得这道题考察的就是找规律, 你找到规律了就能解决了。
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. Número inteiro reverso
Dificuldade: Fácil
Dado um número inteiro com sinal de 32 bits, inverta os dígitos de um número inteiro.
Exemplo 1:
Input: 123
Output: 321
Exemplo 2:
Input: -123
Output: -321
Exemplo 3:
Input: 120
Output: 21
Nota:
Suponha que estejamos lidando com um ambiente que só pode armazenar números inteiros dentro do intervalo de inteiros assinados de 32 bits: [−2<sup>31</sup>, 2<sup>31 </sup>− 1]. Para efeitos deste problema, suponha que sua função retorne 0 quando o número inteiro invertido estourar.
Solução
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;
}
}

Revisão
这篇文章讲的是Pure-singleton设计模式和Semi-singleton设计模式:
https://dandan2009.github.io/2019/10/24/design-patterns-by-tutorials-the-power-of-OOP-part-2/
Dicas
célula talbeView上有视图正在动画,这时如果调用reloadData方法,动画会被打断,有没有方法被打断? 目前还没找到方法
Compartilhar
这两天把Mac系统升级到了10.15,xcode升级到了xcod e11.1,但是遇到了一些问题,在电脑不联网的情况模拟器不能安装A PP或假死,然后Charles不联网的情况下会启动很慢,如果你的电脑是开发主力,建议谨慎升级。不知道是我的环境问题还是新系统问题。
What to read next
Want more posts about ARTS?
Posts in the same category are usually the best next step for reading more on this topic.
View same categoryWant to keep following #iOS?
Tags are useful for related tools, specific problems, and similar troubleshooting notes.
View same tagWant to explore another direction?
If you are not sure what to read next, return to the homepage and start from categories, topics, or latest updates.
Back home