原创 位運算的奇淫巧技

轉載自:https://www.zhihu.com/question/38206659/answer/763034261 計算機中的數在內存中都是以二進制形式進行存儲的,用位運算就是直接對整數在內存中的二進制位進行操作,因此其執行

原创 1085 Perfect Sequence (25分)/二分法

題目描述 時間限制: 200 ms 內存限制: 64 MB 解析 暴力法 暴力法即兩層循環查找,時間複雜度爲O(n2),規模105,時間限制200ms,定然超時。 遞歸 對於排序後的數組,若a[r]<=a[l]*p不滿足,則問題

原创 605. Can Place Flowers

題目描述 Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers ca

原创 1361. Validate Binary Tree Nodes/樹的判斷

題目描述 You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChil

原创 1362. Closest Divisors

題目描述 Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num

原创 1125 Chain the Ropes (25分)/數學分析/排序

題目描述 分析 剛開始都沒太懂題目的意思…   繩子每次打結長度都會減小到原來的一半,那麼打結的順序會影響到最終的長度。我們先來看3段繩子的例子:   3段長度分別爲a,b,c,那麼總長爲((a+b)/2+c)/2=a/4+b/

原创 最短路徑之Dijkstra算法

注:本文總結自《算法筆記》 介紹 “迪傑斯特拉算法”,解決單源最短路問題,即給定圖G和起點s,計算s到達其他每個頂點的最短距離的問題。 基本思想:對圖G(V,E)設置集合S,存放已被訪問的頂點,然後每次從集合V-S(V減S)中選擇

原创 1.Two Sum/hash map

題目描述 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You

原创 1153 Decode Registration Card of PAT (25分)/排序

題目描述 題目大意 題目大意:給出一組學生的准考證號和成績,准考證號包含了考試等級,考場號,日期,和個人編號信息,並有三種查詢方式 查詢一:給出考試等級,找出該等級的考生,按照成績降序,准考證升序排序。 查詢二:給出考場號,統計

原创 219. Contains Duplicate II / 映射表的使用、空間換時間

題目描述 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the ar

原创 1134 Vertex Cover (25分)/圖的特殊存儲

題目描述 解析 我的思路是數邊數——根據給定幾個頂點,數出以這些頂點爲端點的總邊數,如果總邊數與圖的總邊數相等,那麼就滿足條件。但是由於給出的頂點集合可能有相連的情況,需要去掉重複的邊。但是這一步會將算法時間複雜度增加一個數量

原创 1151 LCA in a Binary Tree (30分)

題目描述 題目大意 給出中序序列和先序序列,再給出兩個點,求這兩個點的最近公共祖先。 分析 我的思路   這題拿到手的話,我想多數人還是有思路的,但是如何選擇一個效率高的算法是關鍵。   我的想法是先“建樹”,但是這個樹結點的

原创 434. Number of Segments in a String

題目描述 Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space

原创 1139 First Contact (30分)/巧用圖的存儲方式

題目描述 題目大意 A想對B表白,A會先找一個同性朋友C,C會找一個B的同性朋友D(D也是C的朋友),讓D給B寫信。給你一對A, B,要求找到所有的僚機C, D(成對)。按C的升序排列(如果C相同,按D升序排列)。 解析   看

原创 107. Binary Tree Level Order Traversal II /BFS

題目描述 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right,