LeetCode - Easy - 637. Average of Levels in Binary Tree

Topic

  • Tree

Description

https://leetcode.com/problems/average-of-levels-in-binary-tree/

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10?510^{-5}10?5 of the actual answer will be accepted.

Example 1:

Input: root = [3,9,20,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].

Example 2:

Input: root = [3,9,20,15,7]
Output: [3.00000,14.50000,11.00000]

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4][1,104].
  • ?231?Node.val?231?1-2^{31} \leqslant Node.val \leqslant 2^{31} - 1?231?Node.val?231?1

Analysis

方法一:BFS

方法二:DFS

Submission

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;import com.lun.util.BinaryTree.TreeNode;public class AverageOfLevelsInBinaryTree {// 方法一:BFSpublic List<Double> averageOfLevels(TreeNode root) {LinkedList<TreeNode> queue = new LinkedList<>();queue.offer(root);List<Double> result = new ArrayList<>();while (!queue.isEmpty()) {int size = queue.size(), originSize = size;long sum = 0;for (int i = 0; i < size; i++) {TreeNode node = queue.poll();sum += node.val;if (node.left != null)queue.offer(node.left);if (node.right != null)queue.offer(node.right);}result.add(sum * 1.0 / originSize);}return result;}// 方法二:DFSpublic List<Double> averageOfLevels2(TreeNode root) {List<long[]> levelSumAndCounts = new ArrayList<>();averageOfLevels2(root, 0, levelSumAndCounts);return levelSumAndCounts.stream().map(a -> a[0] * 1.0 / a[1]).collect(Collectors.toList());}private void averageOfLevels2(TreeNode node, int level, List<long[]> levelSumAndCounts) {if (node == null)return;if (levelSumAndCounts.size() == level) {levelSumAndCounts.add(new long[] {node.val, 1});} else {long[] temp = levelSumAndCounts.get(level);temp[0] += node.val;temp[1]++;}averageOfLevels2(node.left, level + 1, levelSumAndCounts);averageOfLevels2(node.right, level + 1, levelSumAndCounts);}
}

Test

import static org.junit.Assert.*;import java.util.List;import org.junit.Test;import com.lun.util.BinaryTree;
import com.lun.util.BinaryTree.TreeNode;public class AverageOfLevelsInBinaryTreeTest {private TreeNode bigRoot = BinaryTree.integers2BinaryTree(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2147483647,2147483647,2147483647,2147483643,2147483645,2147483646,2147483646, //2147483624,2147483646,2147483647,2147483646,2147483647,2147483647,2147483631,2147483647,2147483647,2147483647, //2147483647,2147483631,2147483647,2147483647,2147483647,2147483647,2147483646,2147483646,2147483614,2147483646, //2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646,2147483647,2147483646, //2147483647,2147483644,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647, //2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644, //2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483644,2147483647, //2147483647,2147483644,2147483647,2147483647,2147483644,2147483647,2147483647,2147483642,2147483647,2147483647, //2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647, //2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483636,2147483647,2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647, //2147483647,2147483647,2147483647,2147483647,2147483647,2147483636,2147483647,2147483647,2147483647,2147483647, //2147483647,2147483647,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483647,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642,2147483642, //2147483642,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483638,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642,-2147483642, //-2147483643,-2147483642,-2147483642,-2147483642,-2147483642,-2147483598,-2147483647,-2147483647,-2147483647, //-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645, //-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647, //-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647, //-2147483647,-2147483645,-2147483647,-2147483647,-2147483647,-2147483645,-2147483647,-2147483647,-2147483647, //-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647, //-2147483644,-2147483647,-2147483647,-2147483644,-2147483600,-2147483646,-2147483646,-2147483646,-2147483646, //-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483646,-2147483647,-2147483647, //-2147483525,-2147483647,-2147483647,-2147483644,-2147483647,-2147483647,-2147483647,-2147483646,-2147483647, //-2147483336);@Testpublic void test() {AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();double delta = 10e-5;double[] expected = {3.00000, 14.50000, 11.00000};List<Double> result1 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));List<Double> result2 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));for(int i = 0; i < expected.length; i++) {assertEquals(expected[i], result1.get(i), delta);assertEquals(expected[i], result2.get(i), delta);}List<Double> result3 = obj.averageOfLevels(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));double[] expected3 = {2147483647.00000, 2147483647.00000};for(int i = 0; i < expected3.length; i++) {assertEquals(expected3[i], result3.get(i), delta);}List<Double> result4 = obj.averageOfLevels(bigRoot);double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};for(int i = 0; i < expected4.length; i++) {assertEquals(expected4[i], result4.get(i), delta);}}@Testpublic void test2() {AverageOfLevelsInBinaryTree obj = new AverageOfLevelsInBinaryTree();double delta = 10e-5;double[] expected = {3.00000, 14.50000, 11.00000};List<Double> result1 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, null, 15, 7));List<Double> result2 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(3, 9, 20, 15, 7));for(int i = 0; i < expected.length; i++) {assertEquals(expected[i], result1.get(i), delta);assertEquals(expected[i], result2.get(i), delta);}List<Double> result3 = obj.averageOfLevels2(BinaryTree.integers2BinaryTree(2147483647, 2147483647, 2147483647));double[] expected3 = {2147483647.00000, 2147483647.00000};for(int i = 0; i < expected3.length; i++) {assertEquals(expected3[i], result3.get(i), delta);}List<Double> result4 = obj.averageOfLevels2(bigRoot);double[] expected4 = {0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000};for(int i = 0; i < expected4.length; i++) {assertEquals(expected4[i], result4.get(i), delta);}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445674.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445674.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445674.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在CodeBlocks下配置GoogleTest單元測試框架

環境準備 Windows 10Code::Blocks 20.03Google Test 1.7.0CMake 3.11.0 編譯GoogleTest 一、創建一個工作目錄D:\gtest&#xff0c;將剛下載的Google Test 1.7.0、CMake 3.11.0的壓縮包解壓到剛創建的工作目錄。 二、進入CMake文件夾的bin下&#xff0c;運行cmake-gui.exe&…

傻子都能看懂的馬拉車Manacher

Manachers Algorithm 馬拉車算法操作及原理 package advanced_001;public class Code_Manacher {public static char[] manacherString(String str) {char[] charArr str.toCharArray();char[] res new char[str.length() * 2 1];int index 0;for (int i 0; i ! res.len…

簡單暴力到dp的優化(萌新篇)

想寫一系列文章&#xff0c;總結一些題目&#xff0c;看看解決問題、優化方法的過程到底是什么樣子的。 系列問題一&#xff1a;斐波那契數列問題 在數學上&#xff0c;斐波納契數列以如下被以遞歸的方法定義&#xff1a;F(0)0&#xff0c;F(1)1, F(n)F(n-1)F(n-2)&#xff08…

LeetCode - Medium - 114. Flatten Binary Tree to Linked List

Topic TreeDepth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right…

簡單暴力到dp的優化(入門篇)

上篇&#xff0c;我們提到&#xff0c;遇到問題&#xff0c;首先根據定義寫出笨方法&#xff0c;找出依賴關系&#xff08;有些題這一步就不太簡單&#xff0c;要自己歸納關系&#xff09;&#xff0c;然后進行優化&#xff0c;下面&#xff0c;我們通過幾道此方面的經典的&…

簡單暴力到dp的優化(初級篇)

一、一維非腦殘 1 一個只包含A、B和C的字符串&#xff0c;如果存在某一段長度為3的連續子串中恰好A、B和C各有一個&#xff0c;那么這個字符串就是純凈的&#xff0c;否則這個字符串就是暗黑的。例如&#xff1a;BAACAACCBAAA 連續子串"CBA"中包含了A,B,C各一個&am…

ccpc河北大學生程序設計競賽dp小總結

近期題目來自校賽&#xff0c;賽前訓練&#xff0c;省賽熱身&#xff0c;河北ccpc正式比賽。 題目一&#xff1a; 題目描述&#xff1a; 由于第m個臺階上有好吃的薯條&#xff0c;所以薯片現在要爬一段m階的樓梯. 薯片每步最多能爬k個階梯&#xff0c;但是每到了第i個臺階&a…

c語言簡便實現鏈表增刪改查

注&#xff1a;單追求代碼簡潔&#xff0c;所以寫法可能有點不標準。 //第一次拿c開始寫數據結構&#xff0c;因為自己寫的&#xff0c;追求代碼量少&#xff0c;和學院ppt不太一樣。有錯請指出 #include <stdio.h> #include <stdlib.h> #include <string.h>…

第一次課 課上代碼

第一次課內容 學習心態及注意事項 信心 謙虛 腳踏實地 多動手 python簡介 代碼量少&#xff0c;簡介&#xff0c;易上手&#xff0c;語法要求不過于嚴格&#xff0c; Python 庫。 速度慢&#xff0c; 不可加密。 輸出、變量、輸入 數據類型&#xff1a;整數、浮點數…

計算機考研專業課只考一科的學校匯總

下列學校專業課只考1門 &#xff08;每項科目下的學校均按照最新學科評估結果由高到低進行排名&#xff09; C語言程序設計 1. 湖南大學 計算機技術&軟工專碩&#xff08;信息科學與工程學院&#xff09; 2. 中國海洋大學 計算機技術&#xff08;01計算機應用技術方向&am…

數組實現棧

學習了改進&#xff0c;利用define typedef比上次寫的鏈表更容易改變功能&#xff0c;方便維護&#xff0c;代碼更健壯。 大佬別嫌棄&#xff0c;萌新總是很笨&#xff0c;用typedef都想不到。 #include<stdio.h> #include<stdbool.h> #define maxsize 10 typede…

簡單暴力到dp的優化(中級篇)

下面再放三道我比較喜歡的&#xff0c;需要好好寫一下的題。 第一題比較水 1. White Cloud is exercising in the playground. White Cloud can walk 1 meters or run k meters per second. Since White Cloud is tired,it cant run for two or more continuous seconds. Whi…

第二次課 課上代碼

敲一遍&#xff0c;體會每行代碼想表達的意思。 第二講 創建.py文件 數據類型&#xff1a;布爾(and\or\not) 條件判斷語句(if elif else) 列表基礎操作&#xff08;特點、創建、增加元素、len()、下標、py切片&#xff09; >>> 5>4 True >>> 4>5 Fa…

第一次課 優秀作業展示

18級河北師大軟件編程訓練 很多同學非常認真的完成了作業&#xff0c;這里選出比較優秀的作業展示出來。 注&#xff1a;展示順序不是排名 為了尊重同學們的勞動成果&#xff0c;并沒有要代碼&#xff0c;只是截圖展示。 范天祚 &#xff08;傻兔子&#xff09; 熊靜祎&…

dp打開思路:HDU1029 HDU1087 HDU1176?HDU1257 POJ1458(水題不水)

題目&#xff1a;https://vjudge.net/contest/68966#overview HDU - 1029 題意&#xff1a;找出出現次數超過一半的數字 蠢思路&#xff1a;排序找中間 DP&#xff1a;掃一遍一個變量count記錄解出現的次數&#xff0c;是當前解就&#xff0c;否則--&#xff0c;count為負就…

dp打開思路2:POJ2533 HDU1114 HDU1260 HDU1160(水題不水)

題目&#xff1a;https://vjudge.net/contest/68966#overview POJ2533 最長上升子序列&#xff0c;很平常的題&#xff0c;但是維持單調隊列二分還是值得一貼的&#xff0c;O(nlogn) 關鍵思想&#xff1a;出現在單調隊列里的數都在當前接收的數之前&#xff0c;所以找到最小…

二分查找及一般拓展總結

二分-不止是查找哦 二分過程&#xff1a;首先&#xff0c;假設表中元素是按升序排列&#xff0c;將表中間位置記錄的關鍵字與查找關鍵字比較&#xff0c;如果兩者相等&#xff0c;則查找成功&#xff1b;否則利用中間位置記錄將表分成前、后兩個子表&#xff0c;如果中間位置記…

第三次課 課上代碼

這次可能比較簡短&#xff0c;這樣也好&#xff0c;可讀性比較強。 別問我為什么&#xff0c;我不會告訴你們我把代碼關了的哼哼。 簡單復習、注意事項及小知識強調講解 作業講解 列表的遍歷 For循環&#xff08;這個參考切片&#xff0c;視頻有詳細講解&#xff0c;一樣的…

排序算法基本介紹及python實現(含詳細注釋)

對數組排序可以說是編程基礎中的基礎&#xff0c;本文對八種排序方法做簡要介紹并用python實現。 代碼中注釋很全&#xff0c;適合復習和萌新學習。這是剛入學自己寫的&#xff0c;可能難免比不上標準的寫法&#xff0c;但是懶得改了。 文末會放和排序相關的基本拓展總結鏈接…

第二次作業 講解及展示

第二次作業&#xff0c;同學們雖然在認真完成&#xff0c;但是或多或少都出了一些錯誤&#xff0c;一班張婷&#xff0c;四班武儀人&#xff0c;六班楊澤宇&#xff0c;八班候雯潔&#xff0c;安錦陽&#xff0c;劉凈圓&#xff0c;這些同學完成的較為出色&#xff0c;錯誤較少…