原创 經典PV問題系列一:解決互斥

這幾天正在學PV,覺得網上的總結都不全面,還是自己歸納一下吧。 一、解決互斥 PV是用來解決互斥和同步問題的。從編程角度來看,PV只是兩個函數的實現,其實不用它們也可以達到互斥的目的。要理解PV,應先理解互斥。這裏細緻的分析了一些解決互斥

原创 UvaOJ 10167 Birthday Cake

直接枚舉法 RE了幾次,原因是用了goto語句,也許Uva不支持goto? 對所有可行解只輸出一個即可。。。Uva評測還挺高級的。 #include <stdio.h> #define N 110 int gcd(int a, i

原创 Leetcode Maximum Subarray

動規求最大一維連續子數組,f[i] 表示以i結尾的子數組和。4ms #define MAX(a, b) ((a) > (b) ? (a) : (b)) int maxSubArray(int* nums, int numsSize) {

原创 經典PV問題系列二:經典詳解

上一節討論了計算機解決互斥問題的方法,這一節我們將正式討論各種PV問題。首先給出信號量和PV操作的定義: struc semaphore { int count; queueType queue; } // 對信號量可以實施的操作:

原创 UvaOJ 729 The Hamming Distance Problem

只需從0開始到2^n枚舉,輸出二進制下1的個數有h個的數 #include <stdio.h> int count1(int x) { int cnt = 0; for ( ; x ; x>>=1) if (x & 1)

原创 Leetcode Pascal's Triangle II

動規求Pascal三角,0ms簡單動規 int* getRow(int rowIndex, int* returnSize) { int i, j; *returnSize = rowIndex + 1; int*

原创 UvaOJ 10098 Generating Fast, Sorted Permutation

生成“下一個排列” 思想還是參見我的這篇文章 UvaOJ 146 ID Codes #include <stdio.h> #include <string.h> #include <stdlib.h> char s[11]; int

原创 Leetcode Letter Combinations of a Phone Number

給出手機上的字符按鍵順序,求所有可能的按出的字符串。簡單dfs搜索,0ms const char* map[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

原创 UvaOJ 592 Island of Logic

此題也是poj 1478 解題思路:將所有語句存成結構體的形式,只需要記錄說話人,被說人,說話肯否定(not),和對象。處理方法就是將所有情況K列舉出來,情況K中包含了每個人的種族a[i]和晝夜w的枚舉,然後用所有語句S進行判斷。如果存在

原创 Leetcode Restore IP Addresses

給定一個數字字符串,求所有可能的點分十進制ip。 顯然是dfs,有一些小細節寫在了註釋裏。0ms。 #include <stdlib.h> #include <string.h> /* @param: char* s 剩下的字符串

原创 經典PV問題系列三:習題歸納

1、另類PV操作問題 問題描述:有一個系統,定義P、V操作如下: P(s): s.count --; if s<0 then 將本進程插入相應隊列末尾等待; V(s): s.count ++; if s<=0 then 從相應等待隊列隊尾

原创 Leetcode Merge Sorted Array

合併兩個數組,假設第一個數組內有足夠的空間。4ms void merge(int* nums1, int m, int* nums2, int n) { int i, j = 0, p = 0; for (i = m-1 ; i >=

原创 UvaOJ 10474 Where is the Marble?

快排+lowerbound 注意lowerbound的寫法即可 #include <stdio.h> #include <stdlib.h> int a[10010], n, x; int cmp(const void* a, cons

原创 LeetCode Linked List Cycle II

找鏈表中的環,如果有則輸出環內第一個元素。 經典面試解法是兩個指針,7ms過。(不知道那些6ms是怎麼辦到的,而且還有40%。。。。) 證明理解清楚。設環中節點n個,環外節點m個,則循環中遍歷了(m + (n - m%n) + m)個節點

原创 Leetcode Balanced Binary Tree

判斷一個二叉樹是否是高度平衡的。高度平衡的定義是:對每一個節點,其左兒子樹與右兒子樹的高度差小於等於1。8ms int height(struct TreeNode* node) { if (!node) return 0;