2017/11/9 Leetcode 日记

566. Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

(给一个矩阵和r, c,将这个矩阵重新排列成r行c列的矩阵,如果不可能则输出原矩阵。)

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int row = nums.size(), col = nums[].size();
if(row * col != r * c){
return nums;
}
vector<vector<int>> num(r, vector<int>(c, ));
for(int i = ; i < row * col; i++){
num[i/c][i%c] = nums[i/col][i%col];
}
return num;
}
};

c++

class Solution:
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
row, col = len(nums), len(nums[])
if row * col != r * c:
return nums
o = r*c num = [[None] * c for _ in range(r)]
for i in range(, o):
num[i//c][i%c] = nums[i//col][i%col] return num

python3

682. Baseball Game

You're now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round's operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

class Solution {
public:
int calPoints(vector<string>& ops) {
int len = ops.size();
int sum = , index = ;
vector<int> op; for(int i = ; i < len; i++){
if (ops[i] == "+"){
op.push_back(op[index-] + op[index-]);
}else if (ops[i] == "C"){
op.pop_back();
}else if (ops[i] == "D"){
op.push_back( * op[index-]);
}else{
op.push_back(getNum(ops[i]));
}
index = op.size();
}
return getSum(op);
}
int getNum(string n){
int num = ;
if(n[] == '-'){
for(int i = , sz = n.size(); i<sz; i++){
num *= ;
num += n[i]-'';
}
num = -num;
}else{
for(int i = , sz = n.size(); i<sz; i++){
num *= ;
num += n[i]-'';
}
}
return num;
}
int getSum(vector<int> op){
int sum = ;
for(int i = , sz = op.size(); i<sz; i++){
sum += op[i];
}
return sum;
}
};

c++

class Solution:
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
OP = []
index =
for op in ops:
if op == '+':
OP.append(OP[index-]+OP[index-])
elif op == 'D':
OP.append(*OP[index-])
elif op == 'C':
OP.pop()
else:
OP.append(int(op))
sum =
for o in OP:
sum += o
return sum

Python3

最新文章

  1. 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍
  2. Vector &amp; ArrayList 的主要区别
  3. [本人开发的游戏] Discuz网页动物园插件1.0Beta发布!让积分流动起来!
  4. 【暑假】[实用数据结构]UVa11235 Frequent values
  5. Linux下安装ACE
  6. Objective-C和C++的区别
  7. cocos2d-x3.0 实现HTTP请求GET、POST
  8. 检测网站挂马程序(Python)
  9. idea启动TOMCAT html 乱码
  10. Android -- 《 最美有物》好看的点赞效果
  11. shibie
  12. Photoshop CC安装与破解方法
  13. Excel列名序号互转
  14. Scala思维导图
  15. (13)Python文件操作
  16. MTK WIFI底部加入返回按钮
  17. 开发Web应用(2)(二十一)
  18. 机器学习进阶-图像梯度运算-Sobel算子 1. cv2.Sobel(使用Sobel算子进行计算) 2. cv2.convertScalerAbs(将像素点进行绝对值的计算)
  19. python 创建一次性,快速的小型web服务
  20. Eclipse-快捷键大全(转载)

热门文章

  1. BZOJ 2083 vector的巧用+二分
  2. 你知道吗?.NET Framework 4.5 五个很棒的特性
  3. 在ASP.NET中备份和还原数据库
  4. python学习笔记(十)之格式化字符串
  5. pytesser模块WindowsError错误解决方法
  6. 【Python学习】request库
  7. [Leetcode] Search in Rotated Sorted Array 系列
  8. C++之 extern C的作用详解
  9. ubuntu 命令配置ip 网关 dns
  10. 003_循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate)的区别