2017/11/9 Leetcode 日记
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:
Integer
(one round's score): Directly represents the number of points you get in this round."+"
(one round's score): Represents that the points you get in this round are the sum of the last twovalid
round's points."D"
(one round's score): Represents that the points you get in this round are the doubled data of the lastvalid
round's points."C"
(an operation, which isn't a round's score): Represents the lastvalid
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 日记的更多相关文章
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
- jingchi.ai 2017.11.25-26 Onsite面试
时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...
随机推荐
- CF745 C 并查集
并查集由于政府不能连通我们可以先按给出的边建立连通块,再将不含有政府的点全部作为一个连通块,边数为(n-1)*n/2然后 贪心地将该连通块与[含政府的.且包含点数最多的]连通块相连,然后由于新增了一些 ...
- linux内核文件系统:proc、tmpfs、devfs、sysfs简要介绍
linux内核文件系统:proc.tmpfs.devfs.sysfs proc:虚拟文件系统,在linux系统中被挂载与/proc目录下.里面的文件包含了很多系统信息,比如cpu负载. 内存.网络配置 ...
- 【BZOJ】1426: 收集邮票 期望DP
[题意]有n种不同的邮票,第i次可以花i元等概率购买到一种邮票,求集齐n种邮票的期望代价.n<=10^4. [算法]期望DP [题解]首先设g[i]表示已拥有i张邮票集齐的期望购买次数,根据全期 ...
- 你不知道的Static
Static静态字段,静态方法,静态代码块 壹 简介 一些场景下会要求一个类的多个实例共享一个成员变量:有时候想定义一些不和具体对象关联.不需要new就调用的方法 举例:Console类的Write ...
- 浮动&定位
本文地址:http://www.cnblogs.com/veinyin/p/7606652.html 浮动和定位能够让我们把一些元素放到理想的位置,当然,相比之下 float 只能浮动到左边或右边, ...
- Spring Boot微服务框架的搭建
(1)spring boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- Sublime之插件的安装(三)
今天在写js的时候,突然想到一个问题就是能不能快速的对齐的插件,当当当~~~sublime这么神器当然有,那就是:Alignment插件 如果你写的代码是这样的: var a = , b =, ccc ...
- 多线程中的超时, 如Socket超时
; ,,, ->$port { print "-->$port\r"; #say "\r"; await Promise.anyof( Promis ...
- [005] unique_sub_string
[Description] Given a string, find the largest unique substring. e.g. str[] = "asdfghjkkjhgf&qu ...
- [转载]FFmpeg完美入门[3] - FFmpeg功能及使用说明
1 ffplay对多媒体的支持能力验证 一.视频3gp 177X144 支持播放,在windows下播放正常,但是在linux下面偶有BUG 如果发现画面无法显示而声音可以播放的情况下可以试着切换全屏 ...