返回介绍

solution / 2100-2199 / 2194.Cells in a Range on an Excel Sheet / README_EN

发布于 2024-06-17 01:03:08 字数 4609 浏览 0 评论 0 收藏 0

2194. Cells in a Range on an Excel Sheet

中文文档

Description

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

  • <col> 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.
  • <row> 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 "<col1><row1>:<col2><row2>", where <col1> represents the column c1, <row1> represents the row r1, <col2> represents the column c2, and <row2> 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:

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:

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 ':'.

Solutions

Solution 1

class Solution:
  def cellsInRange(self, s: str) -> List[str]:
    return [
      chr(i) + str(j)
      for i in range(ord(s[0]), ord(s[-2]) + 1)
      for j in range(int(s[1]), int(s[-1]) + 1)
    ]
class Solution {
  public List<String> cellsInRange(String s) {
    List<String> ans = new ArrayList<>();
    for (char i = s.charAt(0); i <= s.charAt(3); ++i) {
      for (char j = s.charAt(1); j <= s.charAt(4); ++j) {
        ans.add(i + "" + j);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> cellsInRange(string s) {
    vector<string> ans;
    for (char i = s[0]; i <= s[3]; ++i)
      for (char j = s[1]; j <= s[4]; ++j)
        ans.push_back({i, j});
    return ans;
  }
};
func cellsInRange(s string) []string {
  var ans []string
  for i := s[0]; i <= s[3]; i++ {
    for j := s[1]; j <= s[4]; j++ {
      ans = append(ans, string(i)+string(j))
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文