什么值得买面试题(一)

一、假设某区域每天能够看到流星的概率是1/3,请问在30天内能够看到流星的概率是多少?

1-(1-1/3)^30

二、8个球有一个重一点,最少称几次能找出来
答:2次。第一次3:3,第一种情况一样重,则称剩下的2个球,找出重的那个;第二种情况其中3个重,则挑选这3个中的任意2个来称,如果一样重则剩下那个球就是重的。
三、将一个整数数组中的顺序循环后移两位

<?php
$a = array(0,1,2,3,4,5,6,7,8,9,20);
$n = 2;

function move($a,$n)
{
	$count = count($a);
	$tmp = '';
    while($n--)
    {
		$tmp = $a[$count-1];
        for($i=$count-1;$i>=1;$i--)
        {
            $a[$i]=$a[$i-1];
        }
        $a[0]=$tmp;
    }
	return $a;
}

四、将一个单链表反置

<?php

class Node{

  private $value;

  private $next;

  public function __construct($value=null){

    $this->value = $value;

  }

  public function getValue(){

    return $this->value;

  }

  public function setValue($value){

    $this->value = $value;

  }

  public function getNext(){

    return $this->next;

  }

  public function setNext($next){

    $this->next = $next;

  }

}

//遍历,将当前节点的下一个节点缓存后更改当前节点指针 

function reverse($head){

  if($head == null){

    return $head;

  }

  $pre = $head;//注意:对象的赋值

  $cur = $head->getNext();

  $next = null;

  while($cur != null){

    $next = $cur->getNext();

    $cur->setNext($pre);

    $pre = $cur;

    $cur = $next;

  }

  //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head 

  $head->setNext(null);

  $head = $pre;

  return $head;

}

//递归,在反转当前节点之前先反转后续节点 

function reverse2($head){

  if (null == $head || null == $head->getNext()) {

    return $head;

  }

  $reversedHead = reverse2($head->getNext());

  $head->getNext()->setNext($head);

  $head->setNext(null);

  return $reversedHead;

}

function test(){

  $head = new Node(0);

  $tmp = null;

  $cur = null;

  // 构造一个长度为10的链表,保存头节点对象head  

  for($i=1;$i<10;$i++){

    $tmp = new Node($i);

    if($i == 1){

      $head->setNext($tmp);

    }else{

      $cur->setNext($tmp);

    }

    $cur = $tmp;

  }

  //print_r($head);exit;

  $tmpHead = $head;

  while($tmpHead != null){

    echo $tmpHead->getValue().' ';

    $tmpHead = $tmpHead->getNext();

  }

  echo "\n";

  //$head = reverse($head);

  $head = reverse2($head);

  while($head != null){

    echo $head->getValue().' ';

    $head = $head->getNext();

  }

}

test();

?>

运行结果:
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

phpstory

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值