作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/self-dividing-numbers/description/

题目描述

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.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.

题目大意

给了一个数组nums2,以及它的一个子集nums1,找出nums1中的每个数字在nums2中的位置右边第一个比该数字大的数。如果没有就返回-1。

解题方法

直接遍历查找

想法比较淳朴:先从nums2中找到对应的nums1数值的序号,然后从这个序号往又找,看有没有比nums1数字大的。

如果有,把这个数字放到结果里;如果没有,就把-1放到结果里。

class Solution(object):
def nextGreaterElement(self, findNums, nums):
"""
:type findNums: List[int]
:type nums: List[int]
:rtype: List[int]
"""
answer = []
for num1 in findNums:
index = -1
for i,nums2 in enumerate(nums):
if num1 == nums[i]:
index = i
break
while index < len(nums) and num1 >= nums[index]:
index += 1
if index == len(nums):
answer.append(-1)
else:
answer.append(nums[index])
return answer

字典保存位置

注意,题目说的是没有重复,那么可以使用字典保存每个数字出现的位置,这样就能直接定位到要寻找的数字在nums中的位置了,然后我们从这个位置向后寻找第一个比他大的数字即可。

class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
unordered_map<int, int> pos;
const int N = nums.size();
for (int i = 0; i < N; i++) {
pos[nums[i]] = i;
}
vector<int> res;
for (int f : findNums) {
int i = pos[f];
for (; i < N; i++) {
if (nums[i] > f) {
res.push_back(nums[i]);
break;
}
}
if (i == N) {
res.push_back(-1);
}
}
return res;
}
};

日期

2018 年 1 月 16 日
2018 年 11 月 9 日 —— 睡眠可以
2018 年 12 月 28 日 —— 元旦假期到了

【LeetCode】496. Next Greater Element I 解题报告(Python & C++)的更多相关文章

  1. LeetCode 496 Next Greater Element I 解题报告

    题目要求 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  2. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  3. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  4. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  5. 【LeetCode】503. Next Greater Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 单调递减栈 日期 题目地址:https:/ ...

  6. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  7. [LeetCode] 496. Next Greater Element I_Easy tag: Stack

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  8. LeetCode: 496 Next Greater Element I(easy)

    题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...

  9. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. R语言与医学统计图形【6】低级绘图函数

    R语言基础绘图系统 基础绘图包之低级绘图函数--定义坐标轴.图例.文本 低级绘图函数:本身不具备图形绘制能力,只是在已有图形基础上添加元素. 函数 功能 arrows 添加箭头 axis 坐标轴 bo ...

  2. 你不知道的iostat

    1.       作用 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况 ...

  3. EXCEL-批量修改列宽

    WPS:先用鼠标选中一列,然后,长按ctrl键并且用鼠标选中剩余想要统一列宽的列,松开ctrl键,鼠标落在刚选中的任意一列的抬头上,鼠标右键,选择列宽,设置统一列宽即可.

  4. 深入理解mysql锁与事务隔离级别

    一.锁 1.锁的定义     锁即是一种用来协调多线程或进程并发使用同一共享资源的机制 2.锁的分类 从性能上分类:乐观锁和悲观锁 从数据库操作类型上分类:读锁和写锁 从操作粒度上分类:表锁和行锁 2 ...

  5. cephfs文件系统场景

    创建cephfs文件系统: [cephfsd@ceph-admin ceph]$ cd /etc/ceph [cephfsd@ceph-admin ceph]$ ceph fs ls No files ...

  6. 【swift】复制后,为Xcode工程项目重新修改名称

    感谢,参考了另一篇博客:https://www.jianshu.com/p/abf10c9609ef 我做了一些修改,和自己遇到的情况 我用的是繁体的mac,所以下面图片内,鼠标右键点出来的文字(丢到 ...

  7. NERD_commenter快捷键

    快捷键有点多,记不过来,做个备份 1. \cc 注释当前行和选中行 2. \cn 没有发现和\cc有区别 3. \c<空格> 如果被选区域有部分被注释,则对被选区域执行取消注释操作,其它情 ...

  8. Kotlin 学习(1)

    本文出自链接:https://www.jianshu.com/p/ef9584a8ebf8 Kotlin的插件安装: Settings->Plugins->Browse Repositor ...

  9. Can we use function on left side of an expression in C and C++?

    In C, it might not be possible to have function names on left side of an expression, but it's possib ...

  10. vue引入d3

    单页面使用 cnpm install d3 --save-dev 指定版本安装 cnpm install d3@6.3.1 -S <script> import * as d3 from ...