赞
踩
- <?php
-
- function getFirstRightVal($arrData) {
- if (empty($arrData)) {
- return array();
- }
- $intPrevMaxVal = $arrData[0];
- $intPrevVal = $arrData[0];
- $arrRes[$arrData[0]] = -1;
- for ($i = 1; $i < count($arrData); $i++) {
- if ($arrData[$i] > $intPrevMaxVal) {
- foreach ($arrRes as $key=>$val) {
- if ($val == -1) {
- $arrRes[$key] = $arrData[$i];
- }
- }
- $intPrevMaxVal = $arrData[$i];
- } elseif ($arrData[$i] > $intPrevVal) {
- $arrRes[$intPrevVal] = $arrData[$i];
- }
- if (!isset($arrRes[$arrData[$i]])) {
- $arrRes[$arrData[$i]] = -1;
- }
- $intPrevVal = $arrData[$i];
- }
- foreach ($arrRes as $key=>$val) {
- if ($val == -1) {
- unset($arrRes[$key]);
- }
- }
- return $arrRes;
- }
-
- $arrData = array(3, 1, 2, 4, 7, 5, 9);
-
- #$arrData = array(1, 2, 3, 4, 5, 6, 7);
-
- #$arrData = array(6, 5, 4, 3, 2, 1, 0);
-
- $arrData = array(1, 1, 1, 0, 1, 1, 0);
-
- $arrRes = getFirstRightVal($arrData);
-
- print_r($arrRes);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。