Make writing a habit together! This is the 11th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

preface

Our community will continue to include Yi Gu (Netflix growth hacker, author of The Way to Interview for iOS, ACE professional fitness coach). Swift algorithm problem solving sorted into text version to facilitate everyone to learn and read.

We have updated 64 issues of LeetCode algorithm so far, and we will keep the update time and progress (Monday, Wednesday, Friday 9:00 am release). The content of each issue is not much, we hope you can read it on your way to work, there will be a great improvement in long-term accumulation.

Short step, no thousands of miles; No small streams, no rivers and seas, Swift community with you forward. If you have suggestions and comments welcome to leave a message at the end of the article, we will try our best to meet your needs.

Difficulty level: Difficult

1. Describe

Significant numbers (in order) can be broken down into the following parts:

  1. A decimal or whole number
  2. (Optional) One'e''E', followed by an integer

Decimals (in order) can be divided into the following parts:

  1. (Optional) A symbolic character ('+'The '-')
  2. One of the following formats:
    1. At least one digit followed by a dot'. '
    2. At least one digit followed by a dot'. ', followed by at least one digit
    3. A point'. ', followed by at least one digit

Integers (in order) can be divided into the following parts:

  1. (Optional) A symbolic character ('+'The '-')
  2. At least one digit

Some valid number listed below: [” 2 “, “0089”, “0.1”, “+ 3.14”, “4”, “- 9”, “2 e10,” “- 90 e3”, “3 e + 7”, “+ 6 e – 1”, “53.5 e93”, “123.456 e789”]

Invalid part number listed below: [” ABC “, “1 a”, “1 e”, “e3”, “99 e2. 5”, “- 6”, “- + 3”, “95 a54e53”]

Given the string s, return true if s is a valid number.

Example 2.

Example 1

Input: s = 0 Output: trueCopy the code

Example 2

Input: s = "e" Output: falseCopy the code

Example 3

Input: s = "." Output: falseCopy the code

Constraints:

  • 1 <= s.length <= 20
  • sContains only English letters (uppercase and lowercase), digits (0-9), plus'+'A minus sign,The '-', or some'. '

3. The answer

class MinimumPathSum {
    func minPathSum(_ grid: [[Int]]) -> Int {
        guard grid.count ! = 0 && grid[0].count ! = 0 else{
            return 0
        }
    
        let m = grid.count, n = grid[0].count
        var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
        
        for i in 0..<m {
            for j in 0..<n {
                if i = = 0 && j = = 0{
                    dp[i][j] = grid[i][j]
                } else if i = = 0 {
                    dp[i][j] = dp[i][j - 1] + grid[i][j]
                } else if j = = 0 {
                    dp[i][j] = dp[i - 1][j] + grid[i][j]
                } else {
                    dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
                }
            }
        }
        
        return dp[m - 1][n - 1]}}Copy the code
  • Main idea: Iterate through the string and process the point, exponent, number, and symbol use cases separately.
  • Time complexity: O(n)
  • Space complexity: O(1)

Leetcode-swift is a repository for solving problems of this algorithm

Click to go to LeetCode practice

About us

We are jointly maintained by Swift enthusiasts. We will share technical content based on Swift combat, SwiftUI and Swift foundation, as well as collect excellent learning materials.