Skip to content

fuck-algorithm/leetcode-283-move-zeroes

Repository files navigation

LeetCode 283 - 移动零 (Move Zeroes) 算法可视化

这是一个 React + TypeScript 实现的 LeetCode 283 "移动零" 题目的交互式可视化演示应用。

题目描述

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

可视化功能

本应用提供了一个交互式的可视化界面,帮助用户直观地理解"移动零"算法的执行过程:

核心功能

  • 算法动画演示

    • 动态显示数组元素变化(零用灰色表示,非零用蓝色表示)
    • 实时展示快慢指针的位置和移动
    • 高亮显示交换操作
    • 同步显示当前执行的代码行
  • 交互控制

    • 播放/暂停动画
    • 上一步/下一步操作
    • 重置动画
    • 速度调节(0.5x ~ 3x)
  • 自定义和预设

    • 支持用户输入自定义数组
    • 提供多种预设示例
    • 随机生成数组功能
  • 辅助信息

    • 动画进度条
    • 操作次数统计
    • 步骤详细描述
    • 键盘快捷键支持

算法实现

本应用使用了双指针交换法,一种简单高效的算法来解决这个问题:

  1. 使用一个慢指针(slow)来追踪非零元素应该放置的位置
  2. 快指针(fast)遍历数组,当遇到非零元素时,将其与慢指针位置的元素交换,然后慢指针前进一步
  3. 算法结束后,所有非零元素都排在数组前面,所有零都移到了数组末尾

这种方法的时间复杂度为 O(n),空间复杂度为 O(1)。

运行项目

首先确保你已经安装了 Node.js 和 npm,然后按照以下步骤操作:

  1. 克隆项目到本地

    git clone https://github.com/yourusername/leetcode-283-move-zeroes.git
    cd leetcode-283-move-zeroes
    
  2. 安装依赖

    npm install
    
  3. 启动开发服务器

    npm start
    
  4. 在浏览器中访问 http://localhost:3000

键盘快捷键

  • 空格键: 播放/暂停动画
  • 右箭头: 下一步
  • 左箭头: 上一步
  • R键: 重置动画

技术栈

  • React
  • TypeScript
  • CSS3 动画
  • Create React App

适用人群

  • 算法初学者
  • 准备技术面试的求职者
  • 教师/教育工作者(用于教学演示)

许可

MIT

Getting Started with Create React App

This project was bootstrapped with Create React App.

Available Scripts

In the project directory, you can run:

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

npm run build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

npm run eject

Note: this is a one-way operation. Once you eject, you can't go back!

If you aren't satisfied with the build tool and configuration choices, you can eject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

Learn More

You can learn more in the Create React App documentation.

To learn React, check out the React documentation.