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

2017/11/9 Leetcode 日记的更多相关文章

  1. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  3. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  4. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  5. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  6. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  7. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

  8. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  9. jingchi.ai 2017.11.25-26 Onsite面试

    时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...

随机推荐

  1. MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields

    Different query operators in MongoDB treat null values differently. The examples on this page use th ...

  2. 集合框架小结-Collection

    1.集合框架作为处理对象的容器存在,基本接口是Collection,相对于数组而言的话,集合框架只能存储对象,但是长度是可变的.集合框架的关系图如下: 主要的内容是list.set.map, List ...

  3. cocos2dx学习,转摘一些链接

    cocos2d-x学习笔记09:动作2:持续动作 ccBezierConfig 贝塞尔坐标点是相对的 Box2DTestBed很有意思的demo,可惜自己水平有限针对其实现还是没弄明白,以后有时间多学 ...

  4. 【leetcode 简单】第十三题 最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  5. Django之ModelForm(二)-----ModelForm组件

    a.  class Meta:             model,                           # 对应Model的             fields=None,     ...

  6. 重载jquery on方法实现click事件在移动端的快速响应

    额,这个标题取的还真是挺装的... 其实我想表达的是jquery click事件如何在移动端自动转换成touchstart事件. 因为移动端click事件会比touchstart事件慢几拍 移动设备某 ...

  7. perl6检测网站CMS脚本(测试代码)

    代码如下: use HTTP::UserAgent; use JSON::Tiny; my $check-url = 'www.baidu.com'; #say @*ARGS[0]; #检测命令行参数 ...

  8. go 数据变量和操作符

    数据类型 布尔类型 a. var b bool 和 var b bool = true 和 var b = falseb. 操作符 == 和 !=c. 取反操作符: !bd. && 和 ...

  9. 64_q1

    QMsgBox-0-9.20130830git94677dc.fc26.i686.rpm 13-Feb-2017 23:40 40674 QMsgBox-0-9.20130830git94677dc. ...

  10. Nim 游戏、SG 函数、游戏的和

    Nim游戏 Nim游戏定义 Nim游戏是组合游戏(Combinatorial Games)的一种,准确来说,属于“Impartial Combinatorial Games”(以下简称ICG).满足以 ...