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 日记的更多相关文章

  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/13 Leetcode 日记

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

  3. 2017/11/20 Leetcode 日记

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

  4. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  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. 2017.11.21 查询某个字段为null的记录

    注意,不使用 = null, 而是 is null. select fd_username, fd_tenantid, fd_validity from t_user WHERE fd_validit ...

随机推荐

  1. PHP扩展--vld查看opcode代码

    vld安装 wget http://pecl.php.net/get/vld-0.13.0.tgz tar zxvf vld-0.13.0.tgz cd vld-0.13.0 /usr/local/p ...

  2. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

  3. Xcode变量概览-summary

    问题描述 在Xcode中断点调试时,鼠标停留在变量上,就能看到变量的信息.但对于自定义对象,通常Xcode提供的直接信息非常有限,像这样 想要了解这个对象具体的内容,需要展开左边的箭头 当开发者想要知 ...

  4. form表单有条件的提交

    form表单提交数据,比如,积分,score,在0--100之间 var score = $('#score').val(); if(score !=''){ if(isNaN(score)){ la ...

  5. CF148A Insomnia cure

    公主睡前数龙, 每隔k, l, m, n只都会用不同的技能攻击龙. 假定共数了d只龙, 问共有多少龙被攻击了. 思路: 用一个visit数组记录被攻击过的dragon, 最后遍历visit数组统计被攻 ...

  6. 学习webpack

    前言 webpack前端工程中扮演的角色越来越重要,它也是前端工程化很重要的一环.本文将和大家一起按照项目流程学习使用wbepack,妈妈再也不用担心我不会使用webpack,哪里不会看哪里.这是一个 ...

  7. Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)

    最近突然想弄一下phonegap,之前一直是听说,没亲自配置开发过.结果配置过程非常艰难啊.特别是android平台的配置,那叫一个麻烦,网上搜了半天都没找到非常好的资料.文章也都是抄来抄去,最烦的就 ...

  8. Skipping 'Android SDK Tools, revision 24.0.2'; it depends on 'Android SDK Platform-tools, revision 20' which was not installed.

    前几天,同事问我eclipse android sdk怎么不能更新. 更新界面是显示(mirrors.neusoft.edu.cn:80),但是不能更新. 问题描述如下: URL not found: ...

  9. CSS 中 nth-child 和 nth-of-type 的区别

    假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...

  10. 72.xilinx vivado zynq vdma仿真及应用详解(一)

    很多人用zynq平台做视频图像开发,但是对vdma了解比较少,上手起来稍微有些困难,我针对这一现象,做了一个基于vivado和modelsim的仿真和应用测试工程,并写篇文章做些介绍,希望能对大家有帮 ...