Skip to content

Commit

Permalink
修复 checkOutOfRange 函数和一些相应的优化
Browse files Browse the repository at this point in the history
修复 checkOutOfRange 函数
优化 insert 函数
修改和 checkOutOfRange 函数相关的逻辑判断
  • Loading branch information
git-zjx authored Mar 29, 2019
1 parent cee7915 commit 1d6064b
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions php/05_array/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function checkIfFull()
*/
private function checkOutOfRange($index)
{
if($index > $this->length+1) {
return true;
if ($index >= $this->length) {
return true;
}
return false;
}
Expand All @@ -68,18 +68,16 @@ public function insert($index, $value)
{
$index = intval($index);
$value = intval($value);
if($index < 0) {
if ($index < 0) {
return 1;
}
if($this->checkIfFull()) {

if ($this->checkIfFull()) {
return 2;
}
if($this->checkOutOfRange($index)) {
return 3;
}

for($i=$this->length-1;$i>=$index;$i--) {
$this->data[$i+1] = $this->data[$i];
for ($i = $this->length - 1; $i >= $index; $i--) {
$this->data[$i + 1] = $this->data[$i];
}

$this->data[$index] = $value;
Expand All @@ -96,22 +94,21 @@ public function delete($index)
{
$value = 0;
$index = intval($index);
if($index < 0) {
if ($index < 0) {
$code = 1;
return array($code, $value);
return [$code, $value];
}
if($index != $this->length+1 && $this->checkOutOfRange($index)) {
if ($this->checkOutOfRange($index)) {
$code = 2;
return array($code, $value);
return [$code, $value];
}

$value = $this->data[$index];
for($i=$index;$i<$this->length-1;$i++) {
$this->data[$i] = $this->data[$i+1];
for ($i = $index; $i < $this->length - 1; $i++) {
$this->data[$i] = $this->data[$i + 1];
}

$this->length--;
return array(0, $value);
return [0, $value];
}

/**
Expand All @@ -123,23 +120,23 @@ public function find($index)
{
$value = 0;
$index = intval($index);
if($index < 0) {
if ($index < 0) {
$code = 1;
return array($code, $value);
return [$code, $value];
}
if($this->checkOutOfRange($index)) {
if ($this->checkOutOfRange($index)) {
$code = 2;
return array($code, $value);
return [$code, $value];
}
return array(0, $this->data[$index]);
return [0, $this->data[$index]];
}

public function printData()
{
$format = "";
for($i=0;$i<$this->length;$i++) {
$format .= "|".$this->data[$i];
for ($i = 0; $i < $this->length; $i++) {
$format .= "|" . $this->data[$i];
}
print($format."\n");
print($format . "\n");
}
}
}

0 comments on commit 1d6064b

Please sign in to comment.