2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记
496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> tNums;
for(int i = , sz = findNums.size(); i < sz; i++){
bool finded = false;
int index = ;
for(int j = findN(findNums[i], nums), nsz = nums.size(); j < nsz; j++){
if(nums[j] > findNums[i]){
index = j;
break;
}
}
if(index == ) tNums.push_back(-);
else tNums.push_back(nums[index]);
}
return tNums;
}
// return index of nums[k] == num
int findN(int num, vector<int>& nums){
for(int i = , sz = nums.size(); i < sz; i++){
if(nums[i] == num){
return i;
}
}
return -;
}
};
c++
513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.
(BFS)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> leaves;
leaves.push(root);
while(!leaves.empty()){
TreeNode *temp = leaves.front();leaves.pop();
if(!temp->right && !temp->left && leaves.empty()) return temp->val;
if(temp->right)
leaves.push(temp->right);
if(temp->left)
leaves.push(temp->left);
}
}
};
c++
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
list = []
list.append(root)
while(len(list)):
temp = list.pop()
if(temp.right == None and temp.left == None and len(list) == ):
return temp.val
if(temp.right):
list.append(temp.right)
if(temp.left):
list.append(temp.left)
python3
540. Single Element in a Sorted Array
Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once.
(二分搜索)
class Solution {
public:
int singleNonDuplicate(vector<int>& nums) {
int left = , right = nums.size()-;
int mid = (left + right) / ;
if(mid == left) return nums[mid];
while(left < right){
if(mid % == ){
if(Left(mid, nums)) {
right = mid-;
mid = (right + left)/;
}
else if(Right(mid, nums)){
left = mid+;
mid = (left + right)/;
}
else return nums[mid];
}else{
if(Left(mid, nums)){
left = mid+;
mid = (left + right)/;
}else{
right = mid-;
mid = (right + left)/;
}
}
}
return nums[mid];
}
bool Left(int i, vector<int>& nums){
int left = ;
if(i == ) return false;
else if (nums[i] != nums[i-]) return false;
else return true;
}
bool Right(int i, vector<int>& nums){
int right = nums.size()-;
if (i == right) return false;
else if (nums[i] != nums[i+]) return false;
else return true;
}
};
c++
647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
class Solution {
public:
int countSubstrings(string s) {
int left = , right = s.size();
// cout<<right<<endl;
// return traceBack(s, left, right);
int count = ;
for(int i = left; i < right; i++){
for(int j = i; j < right; j++){
bool palindromic = true;
for(int ind = i, end = j; ind <= end; ind++, end--){
if(s[ind] != s[end]) palindromic = false;
}
if(palindromic) count++;
}
}
return count;
}
};
c++
637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> q;
queue<long long> level;
vector<double> ave; q.push(root);
level.push();
long long last = , sum = , num = ;
while(!q.empty()){
TreeNode * temp = q.front();q.pop();
long long tp = level.front(); level.pop(); if(temp->right) {q.push(temp->right);level.push(tp+);}
if(temp->left) {q.push(temp->left);level.push(tp+);} if(tp == last){
sum += temp->val;
num ++;
}else{
ave.push_back((double)sum/(double)num);
sum = ;
num = ;
last = tp;
sum += temp->val;
}
if(q.empty()) ave.push_back((double)sum/(double)num);
}
return ave;
}
};
c++
515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> q;
queue<int> level;
vector<int> ave; if(root == NULL) return ave; q.push(root);
level.push();
int last = , max = -(<<);
while(!q.empty()){
TreeNode * temp = q.front();q.pop();
int tp = level.front(); level.pop(); if(temp->right) {q.push(temp->right);level.push(tp+);}
if(temp->left) {q.push(temp->left);level.push(tp+);} if(tp == last){
if(max < temp->val)
max = temp->val;
}else{
ave.push_back(max);
max = -(<<);
last = tp;
if(max < temp->val)
max = temp->val;
}
if(q.empty()) ave.push_back(max);
}
return ave;
}
};
c++
2017/11/21 Leetcode 日记的更多相关文章
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 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/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 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 ...
- 2017.11.21 查询某个字段为null的记录
注意,不使用 = null, 而是 is null. select fd_username, fd_tenantid, fd_validity from t_user WHERE fd_validit ...
随机推荐
- iOS静态库 ---iOS-Apple苹果官方文档翻译
iOS静态库 ---iOS-Apple苹果官方文档翻译 •什么是库? 库是共享程序代码的方式,一般分为静态库和动态库.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使⽤用就为什 ...
- 【leetcode 简单】第三题 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- NYOJ 208 Supermarket (模拟+并查集)
题目链接 描述 A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Pr ...
- 使用OpenCV和Python进行人脸识别
介绍 人脸识别是什么?或识别是什么?当你看到一个苹果时,你的大脑会立刻告诉你这是一个苹果.在这个过程中,你的大脑告诉你这是一个苹果水果,用简单的语言来说就是识别.那么什么是人脸识别呢?我肯定你猜对了. ...
- python基本数据类型list,tuple,set,dict用法以及遍历方法
1.list类型 类似于java的list类型,数据集合,可以追加元素与删除元素. 遍历list可以用下标进行遍历,也可以用迭代器遍历list集合 建立list的时候用[]括号 import sys ...
- Bootstrap文件上传组件:bootstrap fileinput
为了上传预览pdf与图片特用此插件. 源码以及API地址: bootstrap-fileinput源码:https://github.com/kartik-v/bootstrap-fileinput ...
- thinkphp中的验证器
- redis基础之redis-sentinel(哨兵集群)(六)
前言 redis简单的主从复制在生产的环境下可能是不行的,因为从服务器只能读不能写,如果主服务器挂掉,那么整个缓存系统不能写入了:redis自带了sentinel(哨兵)机制可以实现高可用. redi ...
- gunicorn之日志详细配置
gunicorn的日志配置 gunicorn的日志配置相关的常用参数有4个,分别是accesslog,access_log_format,errorlog,loglevel. accesslog:用户 ...
- Vue-Module
由于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿. 为了解决以上问题,Vuex 允许我们将 store 分割成模块(module) ...