ARTS #033

ARTS #033

Posted by Dan on October 24, 2019

ARTS 033

Algorihm

6. ZigZag Conversion

Difficulty: Medium

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

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

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

Example 1:

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

Example 2:

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

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

Solution

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

Language: 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. Reverse Integer

Difficulty: Easy

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution

Language: 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;
    }
}

Review

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

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

Tips

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

Share

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