0%

Difficult:Medium

題目

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

閱讀全文 »

前言

一、原型模式的作用?
1、基本就是你需要從A的實例得到一份與A內容相同,但是又互不干擾的實例的話,就需要使用原型模式。
2、用原型實例指定創建對象的種類,並且通過拷貝這些原型創建新的對象。這個其實和C++的拷貝構造函數的作用是相似的(但不相同),實際上就是動態抽取 當前對象 運行時 的 狀態。
3、當然有的時候,如果我們並不需要基於現有的對象複製新的對象,或者我們需要的就是一個乾淨的空對象,那麼我的首先還是工廠模式或者抽象工廠模式。

二、為什麼需要原型模式?
1、為什麼不用new直接新建對象,而要用原型模式?
首先,用new新建對像不能獲取當前對象運行時的狀態,其次就算new了新對象,在將當前對象的值複製給新對象,效率也不如原型模式高。
2、為什麼不直接使用拷貝構造函數,而要使用原型模式?
原型模式與拷貝構造函數是不同的概念,拷貝構造函數涉及的類是已知的,原型模式涉及的類可以是未知的(基類的拷貝構造函數只能複制得到基類的對象)。
原型模式生成的新對象可能是一個派生類。拷貝構造函數生成的新對像只能是類本身。原型模式是描述了一個通用方法(或概念),它不管是如何實現的,而拷貝構造則是描述了一個具體實現方法。

閱讀全文 »

Difficult:Medium

題目

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

閱讀全文 »

Difficult:Medium

題目

Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head.

The steps of the insertion sort algorithm:

Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
It repeats until no input elements remain.
The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

leetcode147

閱讀全文 »