ARTS #034

ARTS #034

Posted by Dan on October 31, 2019

ARTS 034

Algorihm

292. Nim Game

Difficulty: Easy

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

Example:

Input: 4
Output: false 
Explanation: If there are 4 stones in the heap, then you will never win the game;
             No matter 1, 2, or 3 stones you remove, the last stone will always be 
             removed by your friend.
Solution

这个是想出来了就简单

Language: Swift

class Solution {
    func canWinNim(_ n: Int) -> Bool {
        
        return (n % 4 != 0)
    }
}

8. String to Integer (atoi)

Difficulty: Medium

Implement <span style="display: inline;">atoi</span> which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.
Solution

中规中矩的解法

Language: Swift


class Solution {
    func myAtoi(_ str: String) -> Int {
        var newStr = str
        for cha in  newStr {//去除空格
            if cha == " " {
                newStr.remove(at:str.startIndex)
            }
            else{
                break;
            }
        }
        
        if newStr.count == 0 {
            return 0;
        }
        
        var sign = 1
        let first = newStr[newStr.startIndex]
        if first == "+" {
            sign = 1
            newStr.remove(at:str.startIndex)
        }
        else if (first == "-"){
            sign = -1;
            newStr.remove(at:str.startIndex)
        }
           
        var result = 0
        var temp = 0
        
        for cha in  newStr {
            if cha < "0" || cha > "9" {
                return result
            }
            
            
            temp = temp * 10 + (Int(String(cha)) ?? 0)
            
            
            result = temp * sign
            if result  > Int(Int32.max)  {
                return Int(Int32.max)
            }
            else if result < Int(Int32.min) {
                return Int(Int32.min)
            }
        }
        
        return result
    }
}

Review

这篇文章讲的是Adapter 适配器设计模式:

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

Tips

我们第一次安装CocoaPods后,执行pod install,会克隆 https://github.com/CocoaPods/Specs master分支到~/.cocoapods/repos目录下,但是由于国内环境问题导致 git clone https://github.com/CocoaPods/Specs 很慢,而且很可能会失败。

解决办法:

可以尝试下这个git clone https://git.coding.net/CocoaPods/Specs.git, 或 https://gitclub.cn/CocoaPods/Specs

然后把Specs目录改名为master即可。~/.cocoapods/repos的目录层级如下: 15731384080733

https://git.coding.net/CocoaPods/Specs.git和 https://gitclub.cn/CocoaPods/Specs都是国内镜像,要快很多,还可以直接下载zip包,然后解压到~/.cocoapods/repos目录即可。

Share

昨天看到一个小伙子因为停车费,堵住了一个停车场大门,事情的起因是,小伙子是早上8点进入的停车场,下午2点离场,停车场的收费标准是停车不足12小时收费5元,但是收费员确要10元,收费员是两个50多岁的老头,我之前也遇到过,停车明明不足12小时却收10元,进场有门禁和摄像;估计很多人也遇到过,但是由于赶时间等原因,很多人不想为了5块钱和他们争吵,但是这次他们却遇到了个较真的;小伙子堵停车场门确实不对,但是小伙子较真的做法还是值得肯定的,生活中要多有这样的人,我们的生活或许会更美好; 在日常生活中我们很多人其实都会遇到类似的这样的事,但是因为我们觉得怕麻烦或得不偿失,而不去维护自己的合法权益,允许一些人践踏规则。比如这次事件对小伙子来说为了5元钱,却浪费了近3个小时,从这个角度讲确实不值,但是如果我们每个人与到这样的事都能站出来较真一下,维护规则,那么我们的生活会少很多藏污纳垢的地方。