Leetcode PHP solution — D52 496. Next greater element I


D52 496. Next Greater Element I

Title Link

496. Next Greater Element I

Title Analysis

Given two arrays, the inner elements are not repeated.

Array 1 is a subset of array 2, which returns the largest element to the right of each element in array 1 in the corresponding position of array 2.


It can only be traversed one by one.

Final code

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];
            $result[] = $greater;
        return $result;

If you think this article is useful to you, you are welcome to use AI power generation support.

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]