原创 Leetcode Median of two sorted arrays解題

Q4 Median of two sorted arrays 首先奉上題目: There are two sorted arrays nums1 and nums2 of size m and n respectively.

原创 Leetcode 初探

Add Two Numbers 第一次在leetcode上面做題,選了兩道最簡單的題目來試試手,回憶一下cpp的寫法。寫了一個暑假的python之後,在cpp語法上稍微有點不太習慣,還好,做完前兩道題就上手了。 這裏主要講一講Qu

原创 Leetcode freedom trail

題目 In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom

原创 Leetcode minimum height trees

題目 For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a roo

原创 Leetcode Triangle

題目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the

原创 Leetcode Unique BST

題目 給定正數n,計算1到n組成不同的BST的個數。 題目分析 BST的特點是左子樹小於根, 右子樹大於根,這是一個遞歸的定義。因此我們也可以採用遞歸的反方向,自下而上的遞推出不同BST的個數。首先計算1個結點的BST個數,接下來計算2個

原创 《算法概論》習題8.8

題目 在精確的4SAT(EXACT 4SAT)問題中,輸入爲一組自居,每個子句都是恰好4個文字的析取,且每個變量最多在每個自居中出現依次。目標是求它的滿足賦值——如果該賦值存在。證明精確的4SAT是NP-完全問題。 題目分析 證明是NP-

原创 Leetcode Longest Increasing Subsequences

題目描述 Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9

原创 Leetcode Arithmetic Slices

題目: A sequence of number is called arithmetic if it consists of at least three elements and if the difference between

原创 Leetcode Ugly numbers

題目 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2,

原创 判斷BST是否合法

題目介紹: 二叉搜索樹(BST)的結構是左子樹的值小於根值,右子樹的值小於根值。 解題思路 二叉搜索樹的定義本身就是遞歸的,因此,在這種情況下,用遞歸的方法去判斷每一個子樹是否合法最恰當。 從上圖可以看出:左子樹的最深右子樹必須小於

原创 Leetcode 股票交易

股票交易系列共有六道題目,難度遞增。 1題 Say you have an array for which the ith element is the price of a given stock on day i. If you we

原创 ZigZag Conversion

題目意思是將字符串變成Z型字符串 這道題一開始我是想着通過計算下一步取的字符所在的位置來構成zigzag字符串。 解題思路: 1. 首先進行特殊情況處理,如果len(字符串長度)小於行數,或者行數等於1輸出它本身 2. 接下來分成s

原创 強連通分量的簡單概括

簡介 對於強連通分量,是只懂得原理,但不知道如何用算法實現求解。今天就對強連通分量的算法進行一個整理,也對強連通分量的實例進行分析。 強連通分量 強連通分量出現在有向有環圖中,每一個連通的分量都可以被當作是強連通分量。如左下圖每一個虛線框

原创 Leetcode Jump Game

題目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element