ARTS #033
ARTS #033
ARTS 033
Algorithme
6. Conversion en zigzag
Difficulté : Moyenne
La chaîne "PAYPALISHIRING" est écrite en zigzag sur un nombre donné de lignes comme ceci : (vous souhaiterez peut-être afficher ce motif dans une police fixe pour une meilleure lisibilité)
P A H N
A P L S I I G
Y I R
Et puis lisez ligne par ligne : "PAHNAPLSIIGYIR"
Écrivez le code qui prendra une chaîne et effectuez cette conversion étant donné un nombre de lignes :
string convert(string s, int numRows);
Exemple 1 :
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Exemple 2 :
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Solution
我觉得这道题考察的就是找规律,你找到规律了就能解决了。
Langue : Swift
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. Entier inversé
Difficulté : Facile
Étant donné un entier signé de 32 bits, inversez les chiffres d’un entier.
Exemple 1 :
Input: 123
Output: 321
Exemple 2 :
Input: -123
Output: -321
Exemple 3 :
Input: 120
Output: 21
Remarque :
Supposons que nous ayons affaire à un environnement qui ne peut stocker que des entiers compris dans la plage d’entiers signés de 32 bits : [−2<sup>31</sup>, 2<sup>31 </sup>− 1]. Pour les besoins de ce problème, supposons que votre fonction renvoie 0 lorsque l’entier inversé déborde.
Solution
Langue : Swift
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;
}
}

Avis
Le singleton pur et semi-singleton :
https://dandan2009.github.io/2019/10/24/design-patterns-by-tutorials-the-power-of-OOP-part-2/
Conseils
talbeView cell 目前还没找到方法
Partager
Le système Mac utilise la version 10.15, xcode et xcod e11.1, il s’agit d’une version e11.1. 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