Your task is to design the basic function of Excel and implement the function of sum formula. Specifically, you need to implement the following functions:

Excel(int H, char W): This is the constructor. The inputs represents the height and width of the Excel form. H is a positive integer, range from 1 to 26. It represents the height. W is a character range from 'A' to 'Z'. It represents that the width is the number of characters from 'A' to W. The Excel form content is represented by a height * width 2D integer array C, it should be initialized to zero. You should assume that the first row of C starts from 1, and the first column of C starts from 'A'.

void Set(int row, char column, int val): Change the value at C(row, column) to be val.

int Get(int row, char column): Return the value at C(row, column).

int Sum(int row, char column, List of Strings : numbers): This function calculate and set the value at C(row, column), where the value should be the sum of cells represented by numbers. This function return the sum result at C(row, column). This sum formula should exist until this cell is overlapped by another value or another sum formula.

numbers is a list of strings that each string represent a cell or a range of cells. If the string represent a single cell, then it has the following format : ColRow. For example, "F7" represents the cell at (7, F).

If the string represent a range of cells, then it has the following format : ColRow1:ColRow2. The range will always be a rectangle, and ColRow1 represent the position of the top-left cell, and ColRow2 represents the position of the bottom-right cell.

Example 1:

Excel(3,"C");
// construct a 3*3 2D array with all zero.
// A B C
// 1 0 0 0
// 2 0 0 0
// 3 0 0 0 Set(1, "A", 2);
// set C(1,"A") to be 2.
// A B C
// 1 2 0 0
// 2 0 0 0
// 3 0 0 0 Sum(3, "C", ["A1", "A1:B2"]);
// set C(3,"C") to be the sum of value at C(1,"A") and the values sum of the rectangle range whose top-left cell is C(1,"A") and bottom-right cell is C(2,"B"). Return 4.
// A B C
// 1 2 0 0
// 2 0 0 0
// 3 0 0 4 Set(2, "B", 2);
// set C(2,"B") to be 2. Note C(3, "C") should also be changed.
// A B C
// 1 2 0 0
// 2 0 2 0
// 3 0 0 6

Note:

  1. You could assume that there won't be any circular sum reference. For example, A1 = sum(B1) and B1 = sum(A1).
  2. The test cases are using double-quotes to represent a character.
  3. Please remember to RESET your class variables declared in class Excel, as static/class variables are persisted across multiple test cases. Please see here for more details.

分析:https://www.cnblogs.com/grandyang/p/7170238.html

我们肯定需要一个二维数组mat来保存数据,然后需要一个map来建立单元格和区域和之间的映射,这里的区域和就是sum函数中的字符串数组表示的内容,可参见题目中的例子,有可能单个单元格或者多个。

我们来看set函数,如果我们改变了某个单元格的内容,那么如果作为结果单元格,那么对应的链接就会断开。比如我们有三个单元格A1, B1, C1,我们设置的关联是A1 + B1 = C1,那么我们改变A1和B1的值都是OK的,C1的值会自动更新。但如果我们改变了C1的值,那么这个关联就不复存在了,Excel中也是这样的。所以我们在改变某个单元格的时候,要将其的关联删除。

我们再来看get函数,我们在获取某个单元格的值的时候,一定要先看其有没有和其他单元格关联,如果有的话,要重新计算一下关联,有可能关联的单元格的值已经发生改变了,那么当前作为结果单元格的值也需要改变;如果该单元格没有任何关联,那么就直接从数组mat中取值即可。

最后看本题的难点sum函数,要根据关联格求出结果格的值,首先这个字符串数组可能有多个字符串,每个字符串有两个可能,一种是单个的单元格,一种是两个单元格中间用冒号隔开。那么我们需要分情况讨论,区别这两种情况的方法就是看冒号是否存在,如果不存在,就说明只有一个单元格,我们将其数字和字母都提取出来,调用get函数,将该位置的值加入结果res中;如果冒号存在,我们根据冒号的位置,分别将两个单元格的字母和数字提取出来,然后遍历这两个单元格之间所有的单元格,调用get函数并将返回值加入结果res中。这个遍历相加的过程可能可以用树状数组来优化,但由于这不是此题的考察重点,所以直接遍历就OK。最后别忘了建立目标单元格和区域字符串数组之间的映射,并返回结果res即可。

 class Excel {
public:
Excel(int H, char W) {
m.clear();
mat.resize(H, vector<int>(W - 'A', ));
} void set(int r, char c, int v) {
if (m.count({r, c})) m.erase({r, c});
mat[r - ][c - 'A'] = v;
} int get(int r, char c) {
if (m.count({r, c})) return sum(r, c, m[{r, c}]);
return mat[r - ][c - 'A'];
} int sum(int r, char c, vector<string> strs) {
int res = ;
for (string str : strs) {
auto found = str.find_last_of(":");
if (found == string::npos) {
char y = str[];
int x = stoi(str.substr());
res += get(x, y);
} else {
int x1 = stoi(str.substr(, (int)found - )), y1 = str[] - 'A';
int x2 = stoi(str.substr(found + )), y2 = str[found + ] - 'A';
for (int i = x1; i <= x2; ++i) {
for (int j = y1; j <= y2; ++j) {
res += get(i, j + 'A');
}
}
}
}
m[{r, c}] = strs;
return res;
} private:
vector<vector<int>> mat;
map<pair<int, char>, vector<string>> m;
};

Design Excel Sum Formula的更多相关文章

  1. [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式

    Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...

  2. excel sum求和遇到的问题及解决

    在偶遇的,借助excel公式sum对一个数字数组进行求和,结果为0,很是诧异,当然原因就是,数组里的数字是“常规”格式,不是“数值”格式,由于系统生成的excel,不方便生成的同时再做格式的设置,于是 ...

  3. MySQL与EXCEL sum sumif sumifs 函数结合_品牌汇总_20161101

    计算一些数不难,整体来说还是要培养自我的逻辑意识,逻辑清楚,代码自然而然就知道,总体上训练自己的逻辑,一个是从用户角度,一个是从产品角度. 用户角度需要考虑的是用户的活跃度,具体又可以细分为用户的注册 ...

  4. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

  8. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  9. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

随机推荐

  1. 001_89C52之_Proteus_ADC0809采集电压

    一)使用ADC0809采集直流电压 1. 第一个数码管显示的是采样输入口 2. 后面三位是采样电压 (二)使用ADC0809进行交流电的采样 1. 先进行交流电降压,即用变压器降压后使用整桥电路进行整 ...

  2. luogu 2114 [NOI2014]起床困难综合症 位运算+贪心

    感觉这个思路非常巧妙啊~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&qu ...

  3. vs2015 调试 无法启动程序

    应用程序输出在这个目录 E:\learn\3dlesson\lesson1_createwindow\build\Debug 项目属性 解决方案: 把项目设为启动项目.

  4. 在Android中使用OpenGL ES开发第(四)节:相机预览

    笔者之前写了三篇Android中使用OpenGL ES入门级的文章,从OpenGL ES的相关概念出发,分析了利用OpenGL ES实现3D绘图的重要的两个步骤:定义形状和绘制形状,简单的绘制了一个三 ...

  5. P1582 倒水,P2158 [SDOI2008]仪仗队——数学,二进制

    有n个瓶子,里面都有一升水,但是只想保留k个瓶子,只能两个瓶子里面的水体积相等时才能倒在一个瓶子里:不能丢弃有水的瓶子:瓶子容量无限: 问需要购买几个额外的瓶子才能满足条件: 因为每个瓶子一开始只有一 ...

  6. 三十、CentOS 7之systemd

    一.系统启动流程 POST --> bootloader  --> MBR工作 --> kernel(initramfs/initrd) --> ro rootfs --> ...

  7. JAVA基础知识|异常

    一.基础知识 处理异常,java提供了一个优秀的解决方案:异常处理机制. java把异常当作对象来处理,所有的异常都是由Throwable继承而来,但在下一层立即分解为两个分支:Error和Excep ...

  8. P1944 最长括号匹配_NOI导刊2009提高(1)

    P1944 最长括号匹配_NOI导刊2009提高 题解 宁愿相信世上有鬼,也不能随便相信某谷题目标签 我想了半天然后看了眼题解,发现用栈来模拟就好了 栈来模拟,还要用到一个bool数组,标记是否已经匹 ...

  9. 二、navicat连接本地数据库以及远程数据库

    本地连接 1.打开navicat 2.连接 最后点击确定就连接成功了: 远程数据库 和上面一样.....

  10. kotlin委托属性

    fun main(arg: Array<String>) { val myClass1 = myClass1() myClass1.name="mycalsss1" v ...