文章 224
评论 11
浏览 167380
Tree - 113. Path Sum II

Tree - 113. Path Sum II

113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 Return: [ [5,4,11,2], [5,8,4,5] ] 思路: 题目意思是求出所有根节点到叶子结点路径和为sum的结果。递归求解,找到叶子节点比较sum就可以 代码: go: /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func pathSum(roo....

Tree - 112. Path Sum

Tree - 112. Path Sum

112. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. 思路: 题目意思是问根节点叶子节点的路径有没有求和之后等于sum,很简单的题目,和257题类似,递归求解。 代码: go: /** * Definition for a binary tr....

Tree - 257. Binary Tree Paths

Tree - 257. Binary Tree Paths

257. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input: 1 / \ 2 3 \ 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 思路: 题目意思是从root节点走到叶子节点一共有多少路径,采用递归求解,和先序遍历类似,找到叶子节点就把路径保存下来。 代码: go: /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func binaryTreePaths(root *TreeNode) []string { var r....

Tree - 226. Invert Binary Tree

Tree - 226. Invert Binary Tree

226. Invert Binary Tree Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 思路: 递归求解翻转每个不为nil的节点 代码: go: /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func invertTree(root *TreeNode) *TreeNode { if root == nil { return root } root.Left, root.Right = root.Right, root.Left invertTree(root.Left) invertTree(root.Right) return root }

Tree - 101. Symmetric Tree

Tree - 101. Symmetric Tree

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / \ 2 2 \ \ 3 3 Note: Bonus points if you could solve it both recursively and iteratively. 思路: 题目意思是判断一棵树是不是对称的,可以用迭代或者递归来做 代码: go: /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *Tr....

Tree - 100. Same Tree

Tree - 100. Same Tree

100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example 1: Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true 思路: 题目很简单,判断两个树是否相同,只要随便使用一种方式遍历一次树就可以,这里使用先序遍历来做。 代码: go: /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ //recursive func isSameTree(p *TreeNode, q....

Tree - 102. Binary Tree Level Order Traversal

Tree - 102. Binary Tree Level Order Traversal

102. Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路: 层序遍历二叉树,这一题和637题很像,637是求每一层的平均值,这一题只让打印出来,可以用bfs做,也可以使用dfs做,这里用bfs做,有递归写法和非递归写法,非递归写法使用了队列在存储下一层要遍历的节点。 代码: go: /** * Definition for a binary tree node. * type TreeNode struct {....

Dynamic Programming - 91. Decode Ways

Dynamic Programming - 91. Decode Ways

91. Decode Ways A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. Example 1: Input: “12” Output: 2 Explanation: It could be decoded as “AB” (1 2) or “L” (12). Example 2: Input: “226” Output: 3 Explanation: It could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6). 思路:....

Dynamic Programming - 213. House Robber II

Dynamic Programming - 213. House Robber II

213. House Robber II You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of mo....

Dynamic Progamming - 198. House Robber

Dynamic Progamming - 198. House Robber

198. House Robber You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob....

Dynamic Programming - 221. Maximal Square

Dynamic Programming - 221. Maximal Square

221. Maximal Square Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 思路: 题目意思是给一个矩阵,找出矩阵中最大的正方形面积,正方形里面全是1。使用动态规划的思想做,子问题就是对于任意一个值为1的网格,去比较网格上一个格和左边一格和左上角的网格所记录的最大正方形边长。然后当前格的最小正方形边长就等于前面三者最小值加一,所以状态转移方程就是dp[i][j] = min{dp[i-1][j-1], dp[i][j-1], dp[i-1][j]}, dp[i][j]代表了第i行第j列位置为正方形的右下角,所能表示的最大正方形边长。初始条件和边界条件是第一行和第一列。 代码: go: func maximalSquare(matrix [][]by....

Dynamic Programming - 174. Dungeon Game

Dynamic Programming - 174. Dungeon Game

174. Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess. The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately. Some of the rooms are guarded by....

Dynamic Programming - 97. Interleaving String

Dynamic Programming - 97. Interleaving String

97. Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbcbcac” Output: true Example 2: Input: s1 = “aabcc”, s2 = “dbbca”, s3 = “aadbbbaccc” Output: false 思路: 题目意思是指如果用s1和s2可以拼出s3就返回true,但注意拼接的时候是存在顺序的,就是用来拼接s1和s2的字符在s3中顺序需要和原来一样。考虑最后一步是s3的最后一个字母该来自于谁,之前的拼接出来的结果是否合法,就等价于一个拼接合法字符串加上s1或者s2最后一个字符等于最终结果,这就把问题退化为求上一个合法字符串拼接的问题,很明显,可以用动态规划来做. 如图: ( )s1....

Dynamic Programming - 72. Edit Distance

Dynamic Programming - 72. Edit Distance

72. Edit Distance Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = “horse”, word2 = “ros” Output: 3 Explanation: horse -> rorse (replace ‘h’ with ‘r’) rorse -> rose (remove ‘r’) rose -> ros (remove ‘e’) Example 2: Input: word1 = “intention”, word2 = “e....

Dynamic Programming - 64. Minimum Path Sum

Dynamic Programming - 64. Minimum Path Sum

64. Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example: Input: [   [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the path 1→3→1→1→1 minimizes the sum. 思路: 题目意思是给一个网格,找出左上角到右下角最短路径和,和62、63、120题很像,都是经典dp最值问题,子问题就是走到当前位置从哪个方向过来的数值最小。 代码: go: // time:....

计网 - tcp和udp(一)

计网 - tcp和udp(一)

传输层为应用层提供通信服务,使用网络层的服务。 传输层的功能: 1. 传输层提供进程和进程之间的逻辑通信,而网络层提供主机之间的逻辑通信。 2. 复用和分用。 3. 传输层对收到的报文进行差错检测。 传输层的两种协议: TCP : 面向连接的传输控制协议TCP 传输数据之前必须建立连接,数据传送结束后要释放连接。不提供广播或多播服务。由于TCP要提供可靠的面向连接的传输服务,因此不可避免的增加了许多开销,确认、流量控制、计时器以及连接管理等。 可靠、面向连接、时延大,适用于大文件 UDP: 无连接的用户数据报协议 传输数据之前不需要建立连接,收到UDP报文后也不需要给出任何确认。 不可靠,无连接,时延小,适用于小文件 传输层的寻址与端口 复用:应用层的所有应用进程都可以通过传输层再传输到网络层。 分用:传输层从网络层收到数据后交付给指明的应用进程。 逻辑端口/软件端口 是传输层的SAP(server access point),标识主机中的应用进程。 端口号长度为两个字节16bit,能表示65536各不同的端口号。 在网络中采用发送方和接收方的套接字组合来识别端口,套接字唯一标识了网络....

操作系统 - 进程

操作系统 - 进程

系统为每一个运行的程序配置一个数据结构,称为进程控制块(PCB),用来描述进程的各种信息(如程序代码存放位置) 进程定义 为了方便操作系统管理多道程序,完成各程序并发执行,引入进程、进程实体的概念。 PCB、程序段、数据段三部分构成进程实体(进程映像) 程序段只要存放的是程序代码,数据段主要存放的是程序运行时使用、产生的运算数据。如全局变量、局部变量、宏定义的常量就存放在数据段内。 一般情况下,把进程实体就简称为进程,例如所谓创建进程,实质上就是创建进程实体中的PCB;而撤销进程,实质就是撤销进程实体中的PCB。注意:PCB是进程存在的唯一标志! 也就是说,进程可以定义为:进程是进程实体的运行过程,是系统进行资源分配和调度的一个独立单位。 PCB: 进程的组织方式 进程的组织讨论的是多个进程之间的组织方式,主要有链接方式和索引方式。 - 链接方式:按照进程状态将PCB分成多个队列,操作系统持有指向各个队列的指针 - 索引方式:根据进程状态的不同,建立几张索引表,操作系统持有指向各个索引表的指针。 进程的特征 进程和程序是两个截然不同的概念,相比 程序,进程有以特征: 进程的状态和转....

Dynamic Programming - 279. Perfect Squares

Dynamic Programming - 279. Perfect Squares

279. Perfect Squares Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example 2: Input: n = 13 Output: 2 Explanation: 13 = 4 + 9. 思路: 题目意思是求能用最少的完全平方表示一个数,有一个四平方和定理,可以在O(n)内解决问题,但是这里选择用动态规划来做,子问题就是一个数是由另一个数加上一个平方数,也就是比如A = B + x^2, 而B由是子问题求解,这样的话,能表示A的最少组合就是B最少组合加一,所以动态转移方程就是:dp[i] = min{dp[ i - j*j] + 1}, (j*j <= i),初始条件是dp[0] = 0....

Dynamic Programming - 120. Triangle

Dynamic Programming - 120. Triangle

120. Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. 思路: 题目意思是给一....

Dynamic Programming - 63. Unique Paths II

Dynamic Programming - 63. Unique Paths II

63. Unique Paths II A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the gri....

Nothing just happens, it's all part of a plan.