SENI #033
SENI #033
SENI 033
Algoritma
6. Konversi ZigZag
Kesulitan: Sedang
String "PAYPALISHIRING" ditulis dalam pola zigzag pada sejumlah baris tertentu seperti ini: (Anda mungkin ingin menampilkan pola ini dalam font tetap agar lebih mudah dibaca)
P A H N
A P L S I I G
Y I R
Dan kemudian baca baris demi baris: "PAHNAPLSIIGYIR"
Tulis kode yang akan mengambil string dan lakukan konversi ini dengan memberikan sejumlah baris:
string convert(string s, int numRows);
Contoh 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Contoh 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
Solusi
我觉得这道题考察的就是找规律,你找到规律了就能解决了。
Bahasa: cepat
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. Bilangan Terbalik
Kesulitan: Mudah
Diberikan bilangan bulat bertanda 32-bit, kebalikan dari digit bilangan bulat.
Contoh 1:
Input: 123
Output: 321
Contoh 2:
Input: -123
Output: -321
Contoh 3:
Input: 120
Output: 21
Catatan:
Asumsikan kita berhadapan dengan lingkungan yang hanya dapat menyimpan bilangan bulat dalam rentang bilangan bulat bertanda 32-bit: [−2<sup>31</sup>, 2<sup>31 </sup>− 1]. Untuk mengatasi masalah ini, asumsikan bahwa fungsi Anda mengembalikan 0 ketika bilangan bulat terbalik meluap.
Solusi
Bahasa: cepat
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;
}
}

Ulasan
这篇文章讲的是Pure-singleton设计模式和Semi-singleton设计模式:
https://dandan2009.github.io/2019/10/24/design-patterns-by-tutorials-the-power-of-OOP-part-2/
Kiat
talbeView cell上有视图正在动画,这时如果调用reloadData方法,动画会被打断,有没有方法被打断? 目前还没找到方法
Bagikan
这两天把Mac系统升级到了10.15,xcode升级到了xcod e11.1, sistem operasi e11.1, sistem operasi e11.1, sistem operasi e11.1, sistem operasi sistem operasi a PP或假死,然后Charles不联网的情况下会启动很慢,如果你的perusahaan asuransi kesehatan.
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