7个流行的强化学习算法及代码实现

news2024/9/24 9:26:42

目前流行的强化学习算法包括 Q-learning、SARSA、DDPG、A2C、PPO、DQN 和 TRPO。 这些算法已被用于在游戏、机器人和决策制定等各种应用中,并且这些流行的算法还在不断发展和改进,本文我们将对其做一个简单的介绍。

1、Q-learning

Q-learning:Q-learning 是一种无模型、非策略的强化学习算法。 它使用 Bellman 方程估计最佳动作值函数,该方程迭代地更新给定状态动作对的估计值。 Q-learning 以其简单性和处理大型连续状态空间的能力而闻名。

下面是一个使用 Python 实现 Q-learning 的简单示例:

 importnumpyasnp
 
 # Define the Q-table and the learning rate
 Q=np.zeros((state_space_size, action_space_size))
 alpha=0.1
 
 # Define the exploration rate and discount factor
 epsilon=0.1
 gamma=0.99
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     whilenotdone:
         # Choose an action using an epsilon-greedy policy
         ifnp.random.uniform(0, 1) <epsilon:
             action=np.random.randint(0, action_space_size)
         else:
             action=np.argmax(Q[current_state])
 
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
 
         # Update the Q-table using the Bellman equation
         Q[current_state, action] =Q[current_state, action] +alpha* (reward+gamma*np.max(Q[next_state]) -Q[current_state, action])
 
         current_state=next_state

上面的示例中,state_space_size 和 action_space_size 分别是环境中的状态数和动作数。 num_episodes 是要为运行算法的轮次数。 initial_state 是环境的起始状态。 take_action(current_state, action) 是一个函数,它将当前状态和一个动作作为输入,并返回下一个状态、奖励和一个指示轮次是否完成的布尔值。

在 while 循环中,使用 epsilon-greedy 策略根据当前状态选择一个动作。 使用概率 epsilon选择一个随机动作,使用概率 1-epsilon选择对当前状态具有最高 Q 值的动作。

采取行动后,观察下一个状态和奖励,使用Bellman方程更新q。 并将当前状态更新为下一个状态。这只是 Q-learning 的一个简单示例,并未考虑 Q-table 的初始化和要解决的问题的具体细节。

2、SARSA

SARSA:SARSA 是一种无模型、基于策略的强化学习算法。 它也使用Bellman方程来估计动作价值函数,但它是基于下一个动作的期望值,而不是像 Q-learning 中的最优动作。 SARSA 以其处理随机动力学问题的能力而闻名。

 importnumpyasnp
 
 # Define the Q-table and the learning rate
 Q=np.zeros((state_space_size, action_space_size))
 alpha=0.1
 
 # Define the exploration rate and discount factor
 epsilon=0.1
 gamma=0.99
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     action=epsilon_greedy_policy(epsilon, Q, current_state)
     whilenotdone:
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
         # Choose next action using epsilon-greedy policy
         next_action=epsilon_greedy_policy(epsilon, Q, next_state)
         # Update the Q-table using the Bellman equation
         Q[current_state, action] =Q[current_state, action] +alpha* (reward+gamma*Q[next_state, next_action] -Q[current_state, action])
         current_state=next_state
         action=next_action

state_space_size和action_space_size分别是环境中的状态和操作的数量。num_episodes是您想要运行SARSA算法的轮次数。Initial_state是环境的初始状态。take_action(current_state, action)是一个将当前状态和作为操作输入的函数,并返回下一个状态、奖励和一个指示情节是否完成的布尔值。

在while循环中,使用在单独的函数epsilon_greedy_policy(epsilon, Q, current_state)中定义的epsilon-greedy策略来根据当前状态选择操作。使用概率 epsilon选择一个随机动作,使用概率 1-epsilon对当前状态具有最高 Q 值的动作。

上面与Q-learning相同,但是采取了一个行动后,在观察下一个状态和奖励时它然后使用贪心策略选择下一个行动。并使用Bellman方程更新q表。

3、DDPG

DDPG 是一种用于连续动作空间的无模型、非策略算法。 它是一种actor-critic算法,其中actor网络用于选择动作,而critic网络用于评估动作。 DDPG 对于机器人控制和其他连续控制任务特别有用。

 importnumpyasnp
 fromkeras.modelsimportModel, Sequential
 fromkeras.layersimportDense, Input
 fromkeras.optimizersimportAdam
 
 # Define the actor and critic models
 actor=Sequential()
 actor.add(Dense(32, input_dim=state_space_size, activation='relu'))
 actor.add(Dense(32, activation='relu'))
 actor.add(Dense(action_space_size, activation='tanh'))
 actor.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 critic=Sequential()
 critic.add(Dense(32, input_dim=state_space_size, activation='relu'))
 critic.add(Dense(32, activation='relu'))
 critic.add(Dense(1, activation='linear'))
 critic.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 # Define the replay buffer
 replay_buffer= []
 
 # Define the exploration noise
 exploration_noise=OrnsteinUhlenbeckProcess(size=action_space_size, theta=0.15, mu=0, sigma=0.2)
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     whilenotdone:
         # Select an action using the actor model and add exploration noise
         action=actor.predict(current_state)[0] +exploration_noise.sample()
         action=np.clip(action, -1, 1)
 
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
 
         # Add the experience to the replay buffer
         replay_buffer.append((current_state, action, reward, next_state, done))
 
         # Sample a batch of experiences from the replay buffer
         batch=sample(replay_buffer, batch_size)
 
         # Update the critic model
         states=np.array([x[0] forxinbatch])
         actions=np.array([x[1] forxinbatch])
         rewards=np.array([x[2] forxinbatch])
         next_states=np.array([x[3] forxinbatch])
 
         target_q_values=rewards+gamma*critic.predict(next_states)
         critic.train_on_batch(states, target_q_values)
 
         # Update the actor model
         action_gradients=np.array(critic.get_gradients(states, actions))
         actor.train_on_batch(states, action_gradients)
 
         current_state=next_state

在本例中,state_space_size和action_space_size分别是环境中的状态和操作的数量。num_episodes是轮次数。Initial_state是环境的初始状态。Take_action (current_state, action)是一个函数,它接受当前状态和操作作为输入,并返回下一个操作。

4、A2C

A2C(Advantage Actor-Critic)是一种有策略的actor-critic算法,它使用Advantage函数来更新策略。 该算法实现简单,可以处理离散和连续的动作空间。

 importnumpyasnp
 fromkeras.modelsimportModel, Sequential
 fromkeras.layersimportDense, Input
 fromkeras.optimizersimportAdam
 fromkeras.utilsimportto_categorical
 
 # Define the actor and critic models
 state_input=Input(shape=(state_space_size,))
 actor=Dense(32, activation='relu')(state_input)
 actor=Dense(32, activation='relu')(actor)
 actor=Dense(action_space_size, activation='softmax')(actor)
 actor_model=Model(inputs=state_input, outputs=actor)
 actor_model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001))
 
 state_input=Input(shape=(state_space_size,))
 critic=Dense(32, activation='relu')(state_input)
 critic=Dense(32, activation='relu')(critic)
 critic=Dense(1, activation='linear')(critic)
 critic_model=Model(inputs=state_input, outputs=critic)
 critic_model.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     done=False
     whilenotdone:
         # Select an action using the actor model and add exploration noise
         action_probs=actor_model.predict(np.array([current_state]))[0]
         action=np.random.choice(range(action_space_size), p=action_probs)
 
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
 
         # Calculate the advantage
         target_value=critic_model.predict(np.array([next_state]))[0][0]
         advantage=reward+gamma*target_value-critic_model.predict(np.array([current_state]))[0][0]
 
         # Update the actor model
         action_one_hot=to_categorical(action, action_space_size)
         actor_model.train_on_batch(np.array([current_state]), advantage*action_one_hot)
 
         # Update the critic model
         critic_model.train_on_batch(np.array([current_state]), reward+gamma*target_value)
 
         current_state=next_state

在这个例子中,actor模型是一个神经网络,它有2个隐藏层,每个隐藏层有32个神经元,具有relu激活函数,输出层具有softmax激活函数。critic模型也是一个神经网络,它有2个隐含层,每层32个神经元,具有relu激活函数,输出层具有线性激活函数。

使用分类交叉熵损失函数训练actor模型,使用均方误差损失函数训练critic模型。动作是根据actor模型预测选择的,并添加了用于探索的噪声。

5、PPO

PPO(Proximal Policy Optimization)是一种策略算法,它使用信任域优化的方法来更新策略。 它在具有高维观察和连续动作空间的环境中特别有用。 PPO 以其稳定性和高样品效率而著称。

 importnumpyasnp
 fromkeras.modelsimportModel, Sequential
 fromkeras.layersimportDense, Input
 fromkeras.optimizersimportAdam
 
 # Define the policy model
 state_input=Input(shape=(state_space_size,))
 policy=Dense(32, activation='relu')(state_input)
 policy=Dense(32, activation='relu')(policy)
 policy=Dense(action_space_size, activation='softmax')(policy)
 policy_model=Model(inputs=state_input, outputs=policy)
 
 # Define the value model
 value_model=Model(inputs=state_input, outputs=Dense(1, activation='linear')(policy))
 
 # Define the optimizer
 optimizer=Adam(lr=0.001)
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     whilenotdone:
         # Select an action using the policy model
         action_probs=policy_model.predict(np.array([current_state]))[0]
         action=np.random.choice(range(action_space_size), p=action_probs)
 
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
 
         # Calculate the advantage
         target_value=value_model.predict(np.array([next_state]))[0][0]
         advantage=reward+gamma*target_value-value_model.predict(np.array([current_state]))[0][0]
 
         # Calculate the old and new policy probabilities
         old_policy_prob=action_probs[action]
         new_policy_prob=policy_model.predict(np.array([next_state]))[0][action]
 
         # Calculate the ratio and the surrogate loss
         ratio=new_policy_prob/old_policy_prob
         surrogate_loss=np.minimum(ratio*advantage, np.clip(ratio, 1-epsilon, 1+epsilon) *advantage)
 
         # Update the policy and value models
         policy_model.trainable_weights=value_model.trainable_weights
         policy_model.compile(optimizer=optimizer, loss=-surrogate_loss)
         policy_model.train_on_batch(np.array([current_state]), np.array([action_one_hot]))
         value_model.train_on_batch(np.array([current_state]), reward+gamma*target_value)
 
         current_state=next_state

6、DQN

DQN(深度 Q 网络)是一种无模型、非策略算法,它使用神经网络来逼近 Q 函数。 DQN 特别适用于 Atari 游戏和其他类似问题,其中状态空间是高维的,并使用神经网络近似 Q 函数。

 importnumpyasnp
 fromkeras.modelsimportSequential
 fromkeras.layersimportDense, Input
 fromkeras.optimizersimportAdam
 fromcollectionsimportdeque
 
 # Define the Q-network model
 model=Sequential()
 model.add(Dense(32, input_dim=state_space_size, activation='relu'))
 model.add(Dense(32, activation='relu'))
 model.add(Dense(action_space_size, activation='linear'))
 model.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 # Define the replay buffer
 replay_buffer=deque(maxlen=replay_buffer_size)
 
 forepisodeinrange(num_episodes):
     current_state=initial_state
     whilenotdone:
         # Select an action using an epsilon-greedy policy
         ifnp.random.rand() <epsilon:
             action=np.random.randint(0, action_space_size)
         else:
             action=np.argmax(model.predict(np.array([current_state]))[0])
 
         # Take the action and observe the next state and reward
         next_state, reward, done=take_action(current_state, action)
 
         # Add the experience to the replay buffer
         replay_buffer.append((current_state, action, reward, next_state, done))
 
         # Sample a batch of experiences from the replay buffer
         batch=random.sample(replay_buffer, batch_size)
 
         # Prepare the inputs and targets for the Q-network
         inputs=np.array([x[0] forxinbatch])
         targets=model.predict(inputs)
         fori, (state, action, reward, next_state, done) inenumerate(batch):
             ifdone:
                 targets[i, action] =reward
             else:
                 targets[i, action] =reward+gamma*np.max(model.predict(np.array([next_state]))[0])
 
         # Update the Q-network
         model.train_on_batch(inputs, targets)
 
         current_state=next_state

上面的代码,Q-network有2个隐藏层,每个隐藏层有32个神经元,使用relu激活函数。该网络使用均方误差损失函数和Adam优化器进行训练。

7、TRPO

TRPO (Trust Region Policy Optimization)是一种无模型的策略算法,它使用信任域优化方法来更新策略。 它在具有高维观察和连续动作空间的环境中特别有用。

TRPO 是一个复杂的算法,需要多个步骤和组件来实现。TRPO不是用几行代码就能实现的简单算法。

所以我们这里使用实现了TRPO的现有库,例如OpenAI Baselines,它提供了包括TRPO在内的各种预先实现的强化学习算法,。

要在OpenAI Baselines中使用TRPO,我们需要安装:

 pip install baselines

然后可以使用baselines库中的trpo_mpi模块在你的环境中训练TRPO代理,这里有一个简单的例子:

 importgym
 frombaselines.common.vec_env.dummy_vec_envimportDummyVecEnv
 frombaselines.trpo_mpiimporttrpo_mpi
 
 #Initialize the environment
 env=gym.make("CartPole-v1")
 env=DummyVecEnv([lambda: env])
 
 # Define the policy network
 policy_fn=mlp_policy
 
 #Train the TRPO model
 model=trpo_mpi.learn(env, policy_fn, max_iters=1000)

我们使用Gym库初始化环境。然后定义策略网络,并调用TRPO模块中的learn()函数来训练模型。

还有许多其他库也提供了TRPO的实现,例如TensorFlow、PyTorch和RLLib。下面时一个使用TF 2.0实现的样例

 importtensorflowastf
 importgym
 
 # Define the policy network
 classPolicyNetwork(tf.keras.Model):
     def__init__(self):
         super(PolicyNetwork, self).__init__()
         self.dense1=tf.keras.layers.Dense(16, activation='relu')
         self.dense2=tf.keras.layers.Dense(16, activation='relu')
         self.dense3=tf.keras.layers.Dense(1, activation='sigmoid')
 
     defcall(self, inputs):
         x=self.dense1(inputs)
         x=self.dense2(x)
         x=self.dense3(x)
         returnx
 
 # Initialize the environment
 env=gym.make("CartPole-v1")
 
 # Initialize the policy network
 policy_network=PolicyNetwork()
 
 # Define the optimizer
 optimizer=tf.optimizers.Adam()
 
 # Define the loss function
 loss_fn=tf.losses.BinaryCrossentropy()
 
 # Set the maximum number of iterations
 max_iters=1000
 
 # Start the training loop
 foriinrange(max_iters):
     # Sample an action from the policy network
     action=tf.squeeze(tf.random.categorical(policy_network(observation), 1))
 
     # Take a step in the environment
     observation, reward, done, _=env.step(action)
 
     withtf.GradientTape() astape:
         # Compute the loss
         loss=loss_fn(reward, policy_network(observation))
 
     # Compute the gradients
     grads=tape.gradient(loss, policy_network.trainable_variables)
 
     # Perform the update step
     optimizer.apply_gradients(zip(grads, policy_network.trainable_variables))
 
     ifdone:
         # Reset the environment
         observation=env.reset()

在这个例子中,我们首先使用TensorFlow的Keras API定义一个策略网络。然后使用Gym库和策略网络初始化环境。然后定义用于训练策略网络的优化器和损失函数。

在训练循环中,从策略网络中采样一个动作,在环境中前进一步,然后使用TensorFlow的GradientTape计算损失和梯度。然后我们使用优化器执行更新步骤。

这是一个简单的例子,只展示了如何在TensorFlow 2.0中实现TRPO。TRPO是一个非常复杂的算法,这个例子没有涵盖所有的细节,但它是试验TRPO的一个很好的起点。

总结

以上就是我们总结的7个常用的强化学习算法,这些算法并不相互排斥,通常与其他技术(如值函数逼近、基于模型的方法和集成方法)结合使用,可以获得更好的结果。

https://avoid.overfit.cn/post/82000e3c65a14403b5e4defae28b703b

作者:Siddhartha Pramanik

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/176395.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

23种设计模式(十九)——迭代器模式【数据结构】

文章目录 意图什么时候使用迭代器真实世界类比迭代器模式的实现迭代器模式的优缺点亦称:Iterator 意图 提供一个对象来顺序访问聚合对象中的一系列数据,而不暴露聚合对象的内部表示。 什么时候使用迭代器 1、当集合背后为复杂的数据结构, 且你希望对客户端隐藏其复杂性时 …

[QMT]05-获取基础行情信息

函数&#xff1a;获取合约基础信息get_instrument_detail(stock_code)1释义获取合约基础信息参数stock_code - string 合约代码返回 dict 数据字典&#xff0c;{ field1 : value1, field2 : value2, ... }&#xff0c;找不到指定合约时返回NoneExchangeID - string 合约市场代码…

零基础学JavaWeb开发(二十)之 spring框架(3)

SpringBean的AOP 1、AOP基本的概念 AOP(Aspect Oriented Programming)是一种面向切面的编程思想。面向切面编程是将程序抽象成各个切面&#xff0c;即解剖对象的内部&#xff0c;将那些影响了多个类的公共行为抽取到一个可重用模块里&#xff0c;减少系统的重复代码&#xff…

二叉树知识锦囊(三)

作者&#xff1a;爱塔居 专栏&#xff1a;数据结构​​​​​​ 作者简介&#xff1a;大三学生&#xff0c;希望和大家一起进步&#xff01; 目录 前言 1. 检查两棵树是否相同。 2. 另一颗树的子树。 3. 翻转二叉树。 4. 判断一颗二叉树是否是平衡二叉树。 5. 对称二叉树。 前…

【Python】Python数据结构之布尔类型(bool)

目录&#xff1a;Python数据结构之布尔类型&#xff08;bool&#xff09;一、布尔说明二、判定三、布尔运算&#xff1a; and, or, not一、布尔说明 Python 中布尔值使用常量True 和 False来表示&#xff1b;注意大小写。比较运算符< > 等返回的类型就是bool类型&#…

C++虚继承,虚基表 ,菱形继承以及解决方法

目录菱形继承形成原因出现二义性变量的内存布局应对方案虚继承 vitrual解决二义性变量内存布局--虚基表感悟关于代码复用等的另一种关系-组合菱形继承形成原因 多继承&#xff0c;呈菱形状 菱形继承代码: class A { public:A() {}int _a ; }; class B :public A { public…

分享131个ASP源码,总有一款适合您

ASP源码 分享131个ASP源码&#xff0c;总有一款适合您 下面是文件的名字&#xff0c;我放了一些图片&#xff0c;文章里不是所有的图主要是放不下...&#xff0c; 131个ASP源码下载链接&#xff1a;https://pan.baidu.com/s/17vXlBvqeYPM5-XUlu5zaAg?pwd3zzi 提取码&#x…

【Qt】如何使用QtCreator向工程添加文件

文章目录一、导读二、盘一盘文件模板&#xff08;2-1&#xff09;添加C/C文件&#xff08;2-2&#xff09;添加Modeling文件&#xff08;2-3&#xff09;添加Qt相关文件&#xff08;2-4&#xff09;添加GLSL相关文件&#xff08;2-5&#xff09;添加其他文件三、总结一、导读 …

【JavaSE专栏3】JDK安装、IntelliJ IDEA安装、配置环境变量

作者主页&#xff1a;Designer 小郑 作者简介&#xff1a;Java全栈软件工程师一枚&#xff0c;来自浙江宁波&#xff0c;负责开发管理公司OA项目&#xff0c;专注软件前后端开发&#xff08;Vue、SpringBoot和微信小程序&#xff09;、系统定制、远程技术指导。CSDN学院、蓝桥云…

【苹果相册推】Xcode项目,我们将其命名为mypushchat,以及调试的iOS设备

推荐内容IMESSGAE相关 作者✈️IMEAX推荐内容iMessage苹果推软件 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容1.家庭推内容 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容2.相册推 *** 点击即可查看作者要求内容信息作者✈️IMEAX推荐内容3.日历推 *** …

第二个程序——客户端ClientUI

简介 在我的上一篇文章中&#xff0c;我已经介绍了如何实现“在线聊天室”中的服务器端ServerUI&#xff0c;服务器端作为整个聊天系统的“中继系统”&#xff0c;负责转发用户的信息到聊天室&#xff0c;可以转发给聊天室中的每一个人&#xff08;即&#xff0c;群聊&#xf…

一期Go群问答-并发控制-数据竞争-错误与异常

每周更新Go技术交流群的群问答内容&#xff0c;有需要可发我Go加群讨论学习。 并发控制 waitGroup.done()不是必须写在main方法中吗? 为什么我的协程没有成功等待&#xff1f; 熊&#xff1a;如果用了wait group&#xff0c;请求就直接卡住了&#xff0c;如果只有一个gorou…

Linux C编程一站式学习笔记5

Linux C编程一站式学习笔记 chap5 深入理解函数 文章目录Linux C编程一站式学习笔记 chap5 深入理解函数一.return语句习题二.增量式开发三.递归我猜有递归可视化工具&#xff0c;一搜果真有收获习题GCD(Greatest Common Divisor) 最大公约数Fibonacci相关资源、参考资料嘶&…

在linux中安排mysql

linux安装mysql 检测当前系统中是否安装Mysql数据库 rpm -qa rpm -qa|grep mysql rpm -qa|grep mariadb没有输出就是没有安装 我的这里显示mariadb是安装了的&#xff08;会与mysql冲突&#xff09; 卸载已经安装的软件 rpm -e --nodeps 软件名称 rpm -e --nodeps mariadb-li…

什么是执行董事

一、什么是执行董事执行董事&#xff0c;是指参与经营的董事。作为法定意义上的执行董事&#xff0c;是指规模较小的有限公司在不设立董事会的情况下设立的负责公司经营管理的职务。作为上市公司意义上的执行董事&#xff0c;执行董事并没有明确的法规依据。执行董事和非执行董…

偷偷理解Java和Scala中==和equals()的区别

君霸王&#xff0c;社稷定&#xff0c;君不霸王&#xff0c;社稷不定&#x1f97d; 目录 Java总结 Scala总结 Java中和equals() ---------------------------------------------------------------------------------------------------------------------------------------…

【人工智能原理自学】卷积神经网络:图像识别实战

&#x1f60a;你好&#xff0c;我是小航&#xff0c;一个正在变秃、变强的文艺倾年。 &#x1f514;本文讲解卷积神经网络&#xff1a;图像识别实战&#xff0c;一起卷起来叭&#xff01; 目录一、“卷”二、LeNet-5网络一、“卷” 这节课我们来看如何把卷积运算融入到神经网络…

【青训营】Go语言的基本语法

一、 配置Go语言及其开发环境 Mac配置&#xff1a;http://t.zoukankan.com/zsy-p-6685889.html https://wenku.baidu.com/view/8aeec92b15fc700abb68a98271fe910ef12daeaf.html?wkts1673764660043&bdQuery%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AEgopathmac 二、基础语法 p…

避免用Apache Beanutils进行属性的copy。why?让我们一起一探究竟。

在实际的项目开发中&#xff0c;对象间赋值普遍存在&#xff0c;随着双十一、秒杀等电商过程愈加复杂&#xff0c;数据量也在不断攀升&#xff0c;效率问题&#xff0c;浮出水面。 问&#xff1a;如果是你来写对象间赋值的代码&#xff0c;你会怎么做&#xff1f; 答&#xf…

05 |「链表」刷题

前言 前言&#xff1a;链表面试高频题。 文章目录前言一. 基础回顾二. 高频考题1. 例题1&#xff09;题目链接&#xff08;LeetCode 206 反转链表&#xff09;2&#xff09; 算法思路3&#xff09;源码剖析4&#xff09;时间复杂度2. 习题一. 基础回顾 参考上一讲&#xff1a; …