栈 API push pop min实现

本文介绍了一种使用两个栈来实现数据结构,能够在常数时间内获取栈中最小元素的方法。通过维护一个数据栈和一个辅助的最小值栈,确保每次压栈和弹栈操作的同时更新最小值栈,从而实现快速获取最小值的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      class Stack {
       constructor() {
           this.dataStack = []
           this.miniStack = []
       }

       push(num) {
          if(this.dataStack.length === 0) {
              this.dataStack.push(num)
              this.miniStack.push(num)
          } else {
              this.dataStack.push(num)
              if (num < this.miniStack[this.miniStack.length - 1]) {
                  this.miniStack.push(num)
              } else {
                  this.miniStack.push(this.miniStack[this.miniStack.length - 1])
              }
          }
       }

       pop() {
           if(this.dataStack.length === 0) {
              throw new  Error('栈没有数据了')
           }
           this.miniStack.pop()
           return this.dataStack.pop()
       }

       getMin() {
           if(this.dataStack.length === 0) {
               throw new  Error('栈没有数据了')
           }
           return this.miniStack[this.miniStack.length-1]
       }
   }

   var stack = new Stack()
   stack.push(5)
   stack.push(4)
   stack.push(3)
   stack.push(6)
   stack.push(2)
   stack.push(8)

   console.log(stack.getMin()) //2
   stack.pop()
   console.log(stack.getMin()) //2
   stack.pop()
   console.log(stack.getMin()) //3

 

### 创建漫游交互场景 #### 准备工作 为了在 Unity 中创建一个有效的漫游交互场景,需先设置好开发环境。确保安装并配置了 SteamVR 插件以及选择了合适的 Unity 版本[^2]。 #### 场景搭建 构建虚拟现实(VR)体验的基础在于精心设计的三维空间。这包括但不限于: - **模型导入**:将所需的建筑结构、家具等资源拖入项目中。 - **材质应用**:为墙壁、地板和其他表面赋予逼真的外观效果。 #### 控制器脚本编写 对于玩家控制而言,`CameraController` 和 `PlayerController` 是核心组件之一。这两个类负责处理用户的输入并将之转换成角色的动作响应[^3]。 ```csharp using UnityEngine; public class CameraController : MonoBehaviour { public float mouseSensitivity = 100f; private void Update() { float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; transform.Rotate(Vector3.up * mouseX); } } ``` ```csharp using UnityEngine; public class PlayerController : MonoBehaviour { public CharacterController controller; public float speed = 12f; private void Update() { float x = Input.GetAxis("Horizontal"); float z = Input.GetAxis("Vertical"); Vector3 move = transform.right * x + transform.forward * z; controller.Move(move * speed * Time.deltaTime); } } ``` #### 实现基本互动功能 通过附加特定行为到对象上可增强用户体验感。例如,允许用户操作房间内的物品或触发事件。这里展示了一个简单的例子——播放动画片段[^1]。 ```csharp using UnityEngine; public class PlayAnimation : MonoBehaviour { public Animator animator; public void Play(string animationName){ animator.Play(animationName); } } ``` 此方法可用于启动任何已定义好的动作序列,如开门关门或是切换灯光状态等。 #### 高特性集成 除了上述基础要素外,还可以加入更多高特性和细节来提升沉浸度,比如动态天气变化、背景音乐管理等功能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值