carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode2194: Cells in a Range on an Excel Sheet #356

Open carloscn opened 1 year ago

carloscn commented 1 year ago

Description

A cell (r, c) of an excel sheet is represented as a string "" where:

denotes the column number c of the cell. It is represented by alphabetical letters. For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.

is the row number r of the cell. The rth row is represented by the integer r. You are given a string s in the format ":", where represents the column c1, represents the row r1, represents the column c2, and represents the row r2, such that r1 <= r2 and c1 <= c2. Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows. Example 1: ![image](https://github.com/carloscn/structstudy/assets/16836611/47f27ac6-a18f-4224-895b-168e74e2369b) Input: s = "K1:L2" Output: ["K1","K2","L1","L2"] Explanation: The above diagram shows the cells which should be present in the list. The red arrows denote the order in which the cells should be presented. Example 2: ![image](https://github.com/carloscn/structstudy/assets/16836611/68dd4e55-45a0-4409-b96a-7d697b4f5b0d) Input: s = "A1:F1" Output: ["A1","B1","C1","D1","E1","F1"] Explanation: The above diagram shows the cells which should be present in the list. The red arrow denotes the order in which the cells should be presented. Constraints: s.length == 5 'A' <= s[0] <= s[3] <= 'Z' '1' <= s[1] <= s[4] <= '9' s consists of uppercase English letters, digits and ':'.
carloscn commented 1 year ago

Analysis

pub fn cells_in_range(s: &str) -> Vec<String>
{
    let mut ret:Vec<String> = vec![];

    if s.is_empty() {
        return ret;
    }

    let in_str:Vec<char> = s.chars().collect();
    let (z, x, z_d, x_d) =
        (in_str[0], in_str[3], in_str[1], in_str[4]);
    let mut j:u8;
    let mut i:u8 = z as u8;
    let mut t:Vec<char> = vec![];
    while i <= x as u8 {
        t.push(i as char);
        j = z_d as u8;
        while j <= (x_d as u8) {
            t.push(j as char);
            ret.push(t.iter().collect());
            t.pop();
            j += 1;
        }
        t.pop();
        i += 1;
    }

    return ret;
}
carloscn commented 1 year ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/1169777 https://github.com/carloscn/structstudy/commit/6aeed2cc41a44da31440712a37d171e18d039ac9