数据结构php语言,PHP 数据结构 算法 三元组 Triplet

/**

* 三元组 Triplet

*

*/

class Triplet

{

private $_data = null;

// 初始化三元组

public function init($val1,$val2,$val3)

{

$this->_data[0] = $val1;

$this->_data[1] = $val2;

$this->_data[2] = $val3;

return true;

}

// 销毁三元组

public function destroy()

{

unset($this->_data);

return true;

}

// 返回第$key的值

public function get($key)

{

if($key < 1 || $key > 3) return false;

return $this->_data[$key - 1];

}

// 设置第$key元的值为$val

public function put($key,$val)

{

if($key < 1 || $key > 3) return false;

$this->_data[$key - 1] = $val;

return true;

}

// 是否按升序排序

public function isAscending()

{

return ($this->_data[0] <= $this->_data[1]) && ($this->_data[1] <= $this->_data[2]);

}

// 是否按降序排序

public function isDescending()

{

return ($this->_data[0] >= $this->_data[1]) && ($this->_data[1] >= $this->_data[2]);

}

// 获取最大值

public function max()

{

return ($this->_data[0] >= $this->_data[1])? ($this->_data[0] >= $this->_data[2])? $this->_data[0] : $this->_data[2] : ($this->_data[1] >= $this->_data[2])? $this->_data[1] : $this->_data[2];

}

// 获取最小值

public function min()

{

return ($this->_data[0] <= $this->_data[1])? ($this->_data[0] <= $this->_data[2])? $this->_data[0] : $this->_data[2] : ($this->_data[1] <= $this->_data[2])? $this->_data[1] : $this->_data[2];

}

}

//

$objTriplet = new Triplet();

echo "init:";var_dump($objTriplet->init(1,2,3)); echo "
";

echo "get 1:";var_dump($objTriplet->get(1)); echo "
";

echo "get 4:";var_dump($objTriplet->get(4)); echo "
"; // false

echo "put 3,4:";var_dump($objTriplet->put(3,4)); echo "
";

echo "max:";var_dump($objTriplet->max()); echo "
";

echo "min:";var_dump($objTriplet->min()); echo "
";

echo "isAscending:";var_dump($objTriplet->isAscending()); echo "
";

echo "isDescending:";var_dump($objTriplet->isDescending()); echo "
";

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值