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 twovalidround's points."D"(one round's score): Represents that the points you get in this round are the doubled data of the lastvalidround's points."C"(an operation, which isn't a round's score): Represents the lastvalidround'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* ...
随机推荐
- 条件转化,2-sat BZOJ 1997
http://www.lydsy.com/JudgeOnline/problem.php?id=1997 1997: [Hnoi2010]Planar Time Limit: 10 Sec Memo ...
- CF839 C 树形DP 期望
给一颗树,求从根出发路径长度的期望是多少. 树形DP 要想清楚期望的计算 /** @Date : 2017-08-12 23:09:41 * @FileName: C.cpp * @Platform: ...
- 不平衡分类学习方法 --Imbalaced_learn
最近在进行一个产品推荐课题时,由于产品的特性导致正负样本严重失衡,远远大于3:1的比例(个人认为3:1是建模时正负样本的一个临界点),这样的样本不适合直接用来建模,例如正负样本的比例达到了50:1,就 ...
- 强制换行CSS样式
语法: word-wrap : normal | break-word 取值: normal :? 默认值.允许内容顶开指定的容器边界 break-word :? 内容将在边界内换行.如果需要,词内换 ...
- jQuery经典面试题及答案精选[转]
这两天有个面试,把这些记在这里. 问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready( ...
- layer弹窗的跳转功能
1,本弹窗直接跳转父页面: @if(session('message')) <script> window.parent.location.reload(); //刷新父页面 var in ...
- 浅谈桶排思想及[USACO08DEC]Patting Heads 题解
一.桶排思想 1.通过构建n个空桶再将待排各个元素分配到每个桶.而此时有可能每个桶的元素数量不一样,可能会出现这样的情况:有的桶没有放任何元素,有的桶只有一个元素,有的桶不止一个元素可能会是2+: 2 ...
- 2017ACM暑期多校联合训练 - Team 2 1001 HDU 6045 Is Derek lying? (模拟)
题目链接 Problem Description Derek and Alfia are good friends.Derek is Chinese,and Alfia is Austrian.Thi ...
- 记一次诡异的bug调试——————关于JDK1.7和JDK1.8中HashSet的hash(key)算法的区别
现象: 测试提了一个bug,我完全复现不了,但是最吊诡的是在其他人的机器上都可以复现.起初以为是SVN合并后出现的冲突,后来经过对比法排查: step 1: 我本地开两个jetty,一个跑合并之前的版 ...
- docker 加速
Docker配置阿里云加速地址 打开阿里云网站https://cr.console.aliyun.com,登陆自己的阿里云账号. 然后只需要在服务器配置docker配置文件,只需要修改"Ex ...