標籤:題目I:Reverse a singly linked list 思路:建立三個指標,一個用來維護鏈表頭,另外兩個再迴圈中,維護head的前一位和儲存反轉的後一位。 逐個反轉,首先反轉前兩個,然後把前兩個看成是一個,繼續迴圈反轉下去。代碼:public class Solution { public ListNode reverseList(ListNode head) {
標籤:For example,Given height = [2,1,5,6,2,3],return 10.解題思路:參考Problem H: Largest Rectangle in a Histogram 第四種思路,或者翻譯版Largest Rectangle in [email protected],JAVA實現如下: public int largestRectangleArea(int[] height) { Stack<Integer>
標籤:Given a 2D binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing all ones and return its area.解題思路:求01矩陣中,全是1的子矩陣的最大面積。把矩陣按照每一行看做是長條圖,可以轉化為上一題,JAVA實現如下:static public int maximalRectangle(char[][] matrix) {if(matrix.length==0
標籤:局部代碼塊:作用:控制變數的生命週期;在程式中,當我們已經使用完 x 後,並且在接下來的代碼中,不會再用到x,那麼就沒必要讓x 在記憶體中佔用空間了,這用情況下,可以使用 局部代碼塊,將x及其所設計到的地區封裝為局部代碼塊,他們在程式執行中順序不變,只是在執行完成後消失。表現形式,以代碼體現如下: class TestDemo { public static void main(String[] args) {
標籤:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->