原创 reverse bit (!!!)

如果只是reverse一個integer 可以由兩種方法 1. 兩個bit交換 typedef unsigned int uint; uint swapBits(uint x, uint i, uint j) { uint lo =

原创 OOD之思路亂髮散

每一個object都含有state和method,因此定義一個object的時候一定要說明他的含有什麼data member,還有什麼method。 多用design pattern,少用inheritance;using interfa

原创 [leetcode] combination (不會做)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 an

原创 composite VS. inheritance

原帖link:http://www.javaworld.com/jw-11-1998/jw-11-techniques.html?page=3 Inheritance: When you establish an inheritance

原创 [leetcode] merge k sort list

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode

原创 expression 5+4*(7-15) or have parenthesis in any order // 波蘭表示法

Answers write code in java /c for expression 5+4*(7-15) or have parenthesis in any order .idea: Use two stack, one for

原创 [leetcode] Best Time to Buy and Sell Stock

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 da

原创 [leetcode] BST與鏈表的相互轉換

1. 鏈表-->BST (借鑑smile的) /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *

原创 [leetcode] Decode Ways (!!)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ...

原创 【leetcode】 4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? F

原创 deep copy 帶有random的linklist

題目:有一個鏈表L,其每個節點有2個指針,一個指針next指向鏈表的下個節點,另一個random隨機指向鏈表中的任一個節點,可能是自己或者爲空,寫一個程序,要求複製這個鏈表的結構並分析其複雜性 解決方法一: O(n)的複雜度,掃面兩邊

原创 [leetcode] integer 與 Roman的轉換

1. integer到roman 利用了數組的位置的信息 class Solution { public: string intToRoman(int num) { // Start typing your C/C

原创 如何用private 構造函數生成instance

static function in class can only access the static data member in the class. private constructor is used to prevent cr

原创 [leetcode] anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 這

原创 [leetcode] Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Giv