Skip to content

excel-表中某个范围内的单元格

仲灏2022-11-11约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/5e1bb5.md for this page in Markdown format</div>

Excel 表中某个范围内的单元格

CategoryDifficultyLikesDislikes
algorithmsEasy (84.66%)15-

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>

Excel 表中的一个单元格 (r, c) 会以字符串 "<col><row>" 的形式进行表示,其中:

  • <col>

    即单元格的列号

    c

    。用英文字母表中的

    字母

    标识。

    • 例如,第 1 列用 'A' 表示,第 2 列用 'B' 表示,第 3 列用 'C' 表示,以此类推。
  • <row> 即单元格的行号 r 。第 r 行就用 整数 r 标识。

给你一个格式为 "<col1><row1>:<col2><row2>" 的字符串 s ,其中 <col1> 表示 c1 列,<row1> 表示 r1 行,<col2> 表示 c2 列,<row2> 表示 r2 行,并满足 r1 <= r2c1 <= c2

找出所有满足 r1 <= x <= r2c1 <= y <= c2 的单元格,并以列表形式返回。单元格应该按前面描述的格式用 字符串 表示,并以 非递减 顺序排列(先按列排,再按行排)。

示例 1:

img

输入:s = "K1:L2"
输出:["K1","K2","L1","L2"]
解释:
上图显示了列表中应该出现的单元格。
红色箭头指示单元格的出现顺序。

示例 2:

img

输入:s = "A1:F1"
输出:["A1","B1","C1","D1","E1","F1"]
解释:
上图显示了列表中应该出现的单元格。 
红色箭头指示单元格的出现顺序。

提示:

  • s.length == 5
  • 'A' <= s[0] <= s[3] <= 'Z'
  • '1' <= s[1] <= s[4] <= '9'
  • s 由大写英文字母、数字、和 ':' 组成

Discussion | Solution


js
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-11 11:30:21
 * @LastEditTime: 2022-11-11 13:58:41
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:
 * @FilePath: /loan-home/Users/izhaong/izhaong/Project_me/leetcode/2194.excel-表中某个范围内的单元格.js
 */
/*
 * @lc app=leetcode.cn id=2194 lang=javascript
 *
 * [2194] Excel 表中某个范围内的单元格
 *
 * https://leetcode.cn/problems/cells-in-a-range-on-an-excel-sheet/description/
 *
 * algorithms
 * Easy (84.66%)
 * Likes:    15
 * Dislikes: 0
 * Total Accepted:    12.3K
 * Total Submissions: 14.5K
 * Testcase Example:  '"K1:L2"'
 *
 * Excel 表中的一个单元格 (r, c) 会以字符串 "<col><row>" 的形式进行表示,其中:
 *
 *
 * <col> 即单元格的列号 c 。用英文字母表中的 字母 标识。
 *
 *
 * 例如,第 1 列用 'A' 表示,第 2 列用 'B' 表示,第 3 列用 'C' 表示,以此类推。
 *
 *
 * <row> 即单元格的行号 r 。第 r 行就用 整数 r 标识。
 *
 *
 * 给你一个格式为 "<col1><row1>:<col2><row2>" 的字符串 s ,其中 <col1> 表示 c1 列,<row1> 表示 r1
 * 行,<col2> 表示 c2 列,<row2> 表示 r2 行,并满足 r1 <= r2 且 c1 <= c2 。
 *
 * 找出所有满足 r1 <= x <= r2 且 c1 <= y <= c2 的单元格,并以列表形式返回。单元格应该按前面描述的格式用 字符串 表示,并以
 * 非递减 顺序排列(先按列排,再按行排)。
 *
 *
 *
 * 示例 1:
 *
 *
 *
 *
 * 输入:s = "K1:L2"
 * 输出:["K1","K2","L1","L2"]
 * 解释:
 * 上图显示了列表中应该出现的单元格。
 * 红色箭头指示单元格的出现顺序。
 *
 *
 * 示例 2:
 *
 *
 *
 *
 * 输入:s = "A1:F1"
 * 输出:["A1","B1","C1","D1","E1","F1"]
 * 解释:
 * 上图显示了列表中应该出现的单元格。
 * 红色箭头指示单元格的出现顺序。
 *
 *
 *
 *
 * 提示:
 *
 *
 * s.length == 5
 * 'A' <= s[0] <= s[3] <= 'Z'
 * '1' <= s[1] <= s[4] <= '9'
 * s 由大写英文字母、数字、和 ':' 组成
 *
 *
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string[]}
 */
// var cellsInRange = function (s) {
//   const [start, end] = s.split(":");
//   const [sa, sw, sd] = /(^[A-Z])(\d+)/.exec(start);
//   const [ea, ew, ed] = /(^[A-Z])(\d+)/.exec(end);
//   let resArr = [];
//   for (let i = sw.charCodeAt(); i <= ew.charCodeAt(); i++) {
//     for (let j = 1; j <= ed - sd + 1; j++) {
//       resArr.push(String.fromCharCode(i) + j);
//     }
//   }
//   return resArr.length;
// };
var cellsInRange = function (s) {
  const colonIndex = s.indexOf(":");
  const sw = s.slice(0, colonIndex)[0];
  const sd = Number(s.slice(1, colonIndex));
  const ew = s.slice(colonIndex + 1)[0];
  const ed = Number(s.slice(colonIndex + 2)[0]);

  let resArr = [];
  for (let i = sw.charCodeAt(); i <= ew.charCodeAt(); i++) {
    for (let j = 1; j <= ed - sd + 1; j++) {
      resArr.push(String.fromCharCode(i) + j);
    }
  }
  return resArr;
};
// @lc code=end