原创 生成隨機數, random 5, random 7

給定一個範圍是1到5的隨機整數生成器,設計一個範圍是1到7的隨機整數生成器。 解法: int rand7() { int vals[5][5] = { { 1, 2, 3, 4, 5 }, { 6

原创 [LeetCode] Determine if two rectangles overlap

Given two axis-aligned rectangles A and B. Write a function to determine if the two rectangles overlap. 從問題的反面入手,先看看什麼情

原创 求三個數的中數 find median of three numbers

int medianOfThreeNums(int A, int B, int C) { if ((A - B) * (C - A) >= 0) return A; else if ((B - A) * (C - B) >=

原创 [LeetCode] Image Overlap 圖像重疊

Two images A and B are given, represented as binary, square matrices of the same size.  (A binary matrix has only 0s an

原创 [LeetCode] Gas Station 加油站問題

相關問題1:https://blog.csdn.net/jiyanfeng1/article/details/39361485 There are N gas stations along a circular route, where

原创 失戀陣線同盟

[description] 莉莉安女學園的少女們,今天也帶着無垢的笑容,穿過等身高的校門。但是,鮮爲人知的是,每個少女都暗戀着另一位同學。而害羞的少女們都沒有對對方表白。也許是怕說出以後,得到的回答不是自己希望聽到的吧。畢竟,大多數人是單

原创 [LeetCode] Insertion Sort List

Sort a linked list using insertion sort. /** * Definition for singly-linked list. * struct ListNode { * int val

原创 [LeetCode] Remove Nth Node From List 刪除鏈表的第N個節點

Given a linked list, remove the nth node from the end of list and return i

原创 [LeetCode] Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, given 1->2->3->4, you should retur

原创 Find subarray with given sum

Given an unsorted array of nonnegative integers, find a continous subarray

原创 船隻數目 number of boats

給定一個數組 float arr [ N ] ,數組中每個數代表一個人的體重。這些人要過河,一條船最多載兩個人,最大載重爲 W 公斤。把這些人運過河,最少要多少條船? 解法: 先對數組排序,然後設置兩個指針,idx1 和 idx2 ,id

原创 [LeetCode] Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and re

原创 Find the distance between two nodes

找出二叉樹中兩個結點間的距離. 下面的算法的思路和lowest common ancestor的思路是一樣的。適用的情況是,給定的兩個節點都存在於二叉樹上,而且一個節點不能是另外一個的父節點。 #include <vector> #inc

原创 Find duplicates in an array

Find duplicates in an array of length n. The array values are in the range of [1, n-1]. 思路:可以用bucket sort 先對數組排序,然後掃描排序

原创 Find common characters in a set of strings

Find the number of letters common in a given set of strings. Note that the given set of strings might have duplicates.