博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2017/11/9 Leetcode 日记
阅读量:4622 次
发布时间:2019-06-09

本文共 3767 字,大约阅读时间需要 12 分钟。

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
> matrixReshape(vector
>& nums, int r, int c) { int row = nums.size(), col = nums[0].size(); if(row * col != r * c){ return nums; } vector
> num(r, vector
(c, 0)); for(int i = 0; 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[0])        if row * col != r * c:            return nums        o = r*c                num = [[None] * c for _ in range(r)]        for i in range(0, 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
& ops) { int len = ops.size(); int sum = 0, index = 0; vector
op; for(int i = 0; i < len; i++){ if (ops[i] == "+"){ op.push_back(op[index-1] + op[index-2]); }else if (ops[i] == "C"){ op.pop_back(); }else if (ops[i] == "D"){ op.push_back(2 * op[index-1]); }else{ op.push_back(getNum(ops[i])); } index = op.size(); } return getSum(op); } int getNum(string n){ int num = 0; if(n[0] == '-'){ for(int i = 1, sz = n.size(); i
op){ int sum = 0; for(int i = 0, sz = op.size(); i
c++
class Solution:    def calPoints(self, ops):        """        :type ops: List[str]        :rtype: int        """        OP = []        index = 0        for op in ops:            if op == '+':                OP.append(OP[index-1]+OP[index-2])            elif op == 'D':                OP.append(2*OP[index-1])            elif op == 'C':                OP.pop()            else:                OP.append(int(op))        sum = 0        for o in OP:            sum += o        return sum
Python3

 

转载于:https://www.cnblogs.com/yoyo-sincerely/p/7808876.html

你可能感兴趣的文章
图的邻接表存储
查看>>
2018 leetcode
查看>>
PHP中获取当前页面的完整URL
查看>>
所谓输入掩码技术,即只有数字键起作用
查看>>
Display对象,Displayable对象
查看>>
安装oracle11G,10G时都会出现:注册ocx时出现OLE初始化错误或ocx装载错误对话框
查看>>
生产环境下正则的应用实例(一)
查看>>
在CentOS7命令行模式下安装虚拟机
查看>>
Arduino可穿戴开发入门教程Arduino开发环境介绍
查看>>
Windows平台flex+gcc词法分析实验工具包
查看>>
3.Python基础 序列sequence
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Linux搭建Nexus3.X构建maven私服
查看>>
NPOI 操作Excel
查看>>
MySql【Error笔记】
查看>>
vue入门
查看>>
JS线程Web worker
查看>>