原创 阿里巴巴測試題 -- 取石子問題

題目: 有n個石子(n<=100),A、B雙方輪流選取,每次取走若干個石子,取走最後一個石子的一方獲勝。要求:第一次不能全部取完;各方每次選取的石子數不能爲0,也不能超過上次對方選擇的石子數。問:如果A先取,那麼第一次應該選幾個

原创 Leetcode -- 34. Search for a Range

題目: Given an array of integers sorted in ascending order, find the starting and ending position of a given target v

原创 阿里筆試 -- 小張和姑娘約會

題目: 小張很多年過年都沒有回家了,這次回家父母給他安排了很多個相親的姑娘,有一個很長的名單,長度爲N。父親負責安排約會,每次隨機的選擇一個要相親的對象,母親負責記錄哪些姑娘已經約會過了。直到和所有的姑娘都約會完一遍以後,這個浩大

原创 Leetcode -- 36. Valid Sudoku

題目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially

原创 Leetcode -- 38. Count and Say

題目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is

原创 LeetCode -- 213. House Robber II

題目: After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will

原创 LeetCode -- 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

原创 查找樹中兩個節點的最低公共祖先

求樹中兩個節點的最低公共祖先 給定一棵樹和兩個節點,求解這兩個節點在樹中的最低公共祖先節點。(劍指Offer) 思路: 從根節點遍歷樹,直到要查找的節點,保存從根節點到要查找的節點的路徑。遍歷兩次樹,即保存了根節點到要查找的兩個節點的兩

原创 LeetCode -- 121. Best Time to Buy and Sell Stock

題目: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted

原创 LeetCode -- 88. Merge Sorted Array

題目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume

原创 LeetCode -- 122. Best Time to Buy and Sell Stock II

題目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to fin

原创 快排、堆排序

快排: 快速排序主要運用了二分的思想,每次選擇一個基準元素,比基準元素打的元素都放在基準元素前面,比基準元素小的元素都放在基準元素後面,這樣不斷遞歸細分,完成排序。 C++代碼實現: void QuickSort(int a[], int

原创 LeetCode -- 63. Unique Paths II

題目: Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would t

原创 LeetCode -- 96. Unique Binary Search Trees

題目: Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3,

原创 LeetCode -- 120. Triangle

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