当前位置: 面试刷题>> 音乐播放表 (经典算法题500道)


题目描述补充

题目:音乐播放表管理

假设你正在开发一个音乐播放应用,该应用需要维护一个音乐播放列表。播放列表支持以下操作:

  1. 添加歌曲:向播放列表中添加一首新的歌曲。
  2. 删除歌曲:从播放列表中删除指定位置的歌曲。
  3. 移动歌曲:将播放列表中指定位置的歌曲移动到另一个指定位置。
  4. 获取歌曲:根据索引位置获取播放列表中的歌曲。
  5. 获取播放列表长度:返回当前播放列表中的歌曲总数。

为了简化问题,我们可以假设每首歌曲用一个唯一的字符串ID表示。

示例代码

以下是使用PHP、Python和JavaScript编写的示例代码,用于实现上述功能。

PHP 示例

class MusicPlaylist {
    private $playlist;

    public function __construct() {
        $this->playlist = [];
    }

    public function addSong($songId) {
        $this->playlist[] = $songId;
    }

    public function removeSong($index) {
        if (isset($this->playlist[$index])) {
            unset($this->playlist[$index]);
            $this->playlist = array_values($this->playlist); // 重新索引
        }
    }

    public function moveSong($fromIndex, $toIndex) {
        if (isset($this->playlist[$fromIndex]) && $toIndex >= 0 && $toIndex < count($this->playlist)) {
            $song = $this->playlist[$fromIndex];
            unset($this->playlist[$fromIndex]);
            array_splice($this->playlist, $toIndex, 0, [$song]);
        }
    }

    public function getSong($index) {
        return isset($this->playlist[$index]) ? $this->playlist[$index] : null;
    }

    public function getPlaylistLength() {
        return count($this->playlist);
    }
}

// 使用示例
$playlist = new MusicPlaylist();
$playlist->addSong("song1");
$playlist->addSong("song2");
$playlist->moveSong(0, 1);
echo $playlist->getSong(1); // 输出: song1
echo $playlist->getPlaylistLength(); // 输出: 2

Python 示例

class MusicPlaylist:
    def __init__(self):
        self.playlist = []

    def add_song(self, song_id):
        self.playlist.append(song_id)

    def remove_song(self, index):
        if 0 <= index < len(self.playlist):
            del self.playlist[index]

    def move_song(self, from_index, to_index):
        if 0 <= from_index < len(self.playlist) and 0 <= to_index < len(self.playlist):
            self.playlist.insert(to_index, self.playlist.pop(from_index))

    def get_song(self, index):
        if 0 <= index < len(self.playlist):
            return self.playlist[index]
        return None

    def get_playlist_length(self):
        return len(self.playlist)

# 使用示例
playlist = MusicPlaylist()
playlist.add_song("song1")
playlist.add_song("song2")
playlist.move_song(0, 1)
print(playlist.get_song(1))  # 输出: song1
print(playlist.get_playlist_length())  # 输出: 2

JavaScript 示例

class MusicPlaylist {
    constructor() {
        this.playlist = [];
    }

    addSong(songId) {
        this.playlist.push(songId);
    }

    removeSong(index) {
        if (index >= 0 && index < this.playlist.length) {
            this.playlist.splice(index, 1);
        }
    }

    moveSong(fromIndex, toIndex) {
        if (fromIndex >= 0 && fromIndex < this.playlist.length && toIndex >= 0 && toIndex < this.playlist.length) {
            const song = this.playlist.splice(fromIndex, 1)[0];
            this.playlist.splice(toIndex, 0, song);
        }
    }

    getSong(index) {
        if (index >= 0 && index < this.playlist.length) {
            return this.playlist[index];
        }
        return null;
    }

    getPlaylistLength() {
        return this.playlist.length;
    }
}

// 使用示例
const playlist = new MusicPlaylist();
playlist.addSong("song1");
playlist.addSong("song2");
playlist.moveSong(0, 1);
console.log(playlist.getSong(1)); // 输出: song1
console.log(playlist.getPlaylistLength()); // 输出: 2

码小课网站中有更多相关内容分享给大家学习,希望这些示例能帮助你更好地理解和实现音乐播放表管理功能。

推荐面试题