【LeetCode】Algorithms 题集(五)
来源:互联网 发布:codelens数据不可用 编辑:程序博客网 时间:2024/05/19 21:02
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; }};
- 【LeetCode】Algorithms 题集(五)
- 【LeetCode】Algorithms 题集(二)
- 【LeetCode】Algorithms 题集(三)
- 【LeetCode】Algorithms 题集(四)
- 【LeetCode】Algorithms 题集(六)
- 【LeetCode】Algorithms 题集(七)
- 【LeetCode】Algorithms 题集(八)
- 【LeetCode】Algorithms 题集(九)
- Leetcode-Algorithms Number Complement(数字补码)
- Leetcode Blog Post Algorithms
- leetcode algorithms: two sum
- LeetCode Algorithms 1
- Leetcode-Algorithms Keyboard Row
- Leetcode-Algorithms Two Sum
- Leetcode-Algorithms Nim Game
- Leetcode-Algorithms Reverse String
- Leetcode-Algorithms Single Number
- LeetCode Algorithms 46. Permutations
- NOIP 2012花匠
- Android对话框AlertDialog
- 支持向量机(SVM)
- 解决win8.1安装VC6.0兼容性问题
- 算法---基数排序
- 【LeetCode】Algorithms 题集(五)
- spring中配置MySql数据源,怎样配置数据库信息
- Java编程算法基础---整除性问题
- PHP第一章之首个页面Hello.html
- Unity相关的平台相关的条件开关
- 关于职业规划,尤其值得我们程序员学习
- 国外程序员整理的 C++ 资源大全
- RS-232总结
- 二叉树后序遍历(非递归)