Merge Two Sorted Lists
題意:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
思路:
合併兩個鏈表,考察的是指針操作。多注意指針是否爲空,指針操作需謹慎。有兩種情況:
1.其中一個鏈表爲空,則直接返回另一個鏈表。
2.合併過程中,一個鏈表已經走到了末尾,即移動到了空指針,但另一個鏈表還剩下一段,則把剩下的這段接到合併後列表的最後
剩下都就是合併過程中的指針的值的比較而已。
代碼:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
/* 有一個爲空,合併後爲非空的另一個鏈表 */
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
ListNode *head = NULL, *cur = NULL;
/* 初始化頭指針 */
if(l1->val < l2->val)
{
head = l1;
l1 = l1->next;
}
else
{
head = l2;
l2 = l2->next;
}
/* 當前指針 */
cur = head;
/* 要兩個鏈表都非空纔可以一直往後合併 */
while(l1 && l2)
{
if(l1->val < l2->val)
{
cur->next = l1;
cur = cur->next;
l1 = l1->next;
}
else
{
cur->next = l2;
cur = cur->next;
l2 = l2->next;
}
}
/* 其中一個爲空之後,把另一個非空的鏈表接到合併後鏈表的最後 */
if(l1) cur->next = l1;
else if(l2) cur->next = l2;
return head;
}
};
Balanced Binary Tree
題意:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
思路:
判斷一棵二叉樹是否是平衡的,只要遞歸判斷左右子樹的高度。如果左右子樹不再平衡,那麼改變條件變量,遞歸返回。
代碼:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
height(root);
return flag;
}
private:
bool flag = true;
int height(TreeNode* root)
{
if(!flag) return -1;
if(!root) return 0;
int l = height(root->left);
int r = height(root->right);
if(abs(l - r) > 1) flag = false;
return (l>r?l:r) + 1;
}
};
Binary Tree Postorder Traversal
題意:
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
思路:
非遞歸地遍歷二叉樹。坑爹...一開始用 if(!cur->left) 的形式來測試空指針...結果一直 runtime error....原來空指針的 bool 值不爲 false 啊....
二叉樹的前序和中序的非遞歸遍歷比較好做,後序比較難想。不過思路還是用 stack 來解決的。我們需要思考一個問題,什麼時候纔可以訪問一個元素(即遍歷時處理到它)?
我們考慮對一個棧頂元素 cur ,如果:
- cur 的左右子樹都是空的,那麼可以對它進行出棧訪問。
- 如果上個出棧的元素(被處理的元素)是 cur 的右子樹,那麼 cur 可以出棧訪問,因爲它的左右子樹都被處理完了。
- 如果上個出棧的元素是 cur 的左子樹,那麼 cur 可以出棧訪問,因爲這意味着 cur 沒有右子樹而且左子樹被處理完了。
代碼:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
stack<TreeNode*> node;
if(root == NULL) return ans;
node.push(root);
TreeNode* last = root;
while(!node.empty())
{
TreeNode* cur = node.top();
if(last == cur->right || last == cur->left || ((cur->right == NULL) && ( cur->left == NULL)))
{
ans.push_back(cur->val);
node.pop();
last = cur;
} else {
if(cur->right != NULL) node.push(cur->right);
if(cur->left != NULL) node.push(cur->left);
}
}
return ans;
}
};
Number of 1 Bits
題意:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
思路:
計算一個無符號 32 位整數的二進制表示有多少個 1. 只要每次取最後一位判斷是否是 1, 然後進行右移操作就好。複雜度 O(32).
代碼:
class Solution {
public:
int hammingWeight(uint32_t n) {
int ans = 0;
for(int i = 0;i < 32;++i)
{
if(n & 1) ans++;
n = n >> 1;
}
return ans;
}
};
Unique Paths
題意:
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:
機器人只能向右走和向下走,那麼我們可以很容易想到,到一個點的路徑數,是不是等於到這個點的左邊的點的路徑數,加上到達這個點的上邊的點的路徑數。
接下來考慮特殊情況,第一列的點,左邊沒有其他點,所以只能通過它上方的點到達。第一行的點,只能通過它左邊的點到達,再想一下,第一行的點的路徑數是不是隻能爲 1?
看到這裏,你是不是想用二維數組?其實沒有必要,因爲我們不需要那麼多狀態的記錄。我們僅僅需要的是上一次迭代的結果(也就是上一行)。假如我用一個長度爲 n 的一維數組保存到達一行的點的路徑數的話,那麼用 m-1 次迭代(爲什麼用 m - 1?),每次迭代時數組一開始保存的就是上一行的計算結果,這時候我只要讓這個點加等於它左邊的點的路徑數,就得到該點的結果了。
代碼:
class Solution {
public:
int uniquePaths(int m, int n) {
int* dp = new int[n];
for(int i = 0;i < n;++i)
dp[i] = 1;
for(int i = 1;i < m;++i)
{
for(int j = 1;j < n;++j)
{
dp[j] += dp[j-1];
}
}
int ans = dp[n-1];
delete []dp;
return ans;
}
};