Leetcode PHP题解--D52 496. Next Greater Element I

栏目: PHP · 发布时间: 6年前

内容简介:给定两个数组,其内元素不重复。数组1是数组2的子集,返回每个在数组1中的元素在数组2对应位置以右最大的元素。

D52 496. Next Greater Element I

题目链接

496. Next Greater Element I

题目分析

给定两个数组,其内元素不重复。

数组1是数组2的子集,返回每个在数组1中的元素在数组2对应位置以右最大的元素。

思路

只能逐个遍历吧。

最终代码

<?php
class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @return Integer[]
     */
    function nextGreaterElement($nums1, $nums2) {
        $result = [];
        foreach($nums1 as $key => $value){
            $greater = -1;
            $start = false;
            for($i = 0; $i<count($nums2); $i++){
                if($nums2[$i] == $value){
                    $start = true;
                }
                if($start && $nums2[$i]>$value){
                    $greater = $nums2[$i];
                    break;
                }
            }
            $result[] = $greater;
        }
        return $result;
    }
}

若觉得本文章对你有用,欢迎用 爱发电 资助。


以上所述就是小编给大家介绍的《Leetcode PHP题解--D52 496. Next Greater Element I》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Dreamweaver CS3 Bible

Dreamweaver CS3 Bible

Joseph W. Lowery / Wiley / May 21, 2007 / $49.99

Book Description Learn to create dynamic, data-driven Web sites using the exciting enhancements in the Dreamweaver CS3 version. You get a thorough understanding of the basics and then progress to l......一起来看看 《Dreamweaver CS3 Bible》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具