[LeetCode] Cells in a Range on an Excel Sheet

2194. Cells in a Range on an Excel Sheet

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<string> cellsInRange(string s) {
char r1 = s[0], r2 = s[3];
char c1 = s[1], c2 = s[4];
vector<string> res;
for(char row = r1; row <= r2; row++) {
for(char col = c1; col <= c2; col++) {
string tmp = string(1,row) + string(1,col);
res.push_back(tmp);
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/06/PS/LeetCode/cells-in-a-range-on-an-excel-sheet/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.