宽度优先搜索,之前TLE是因为写成了深度优先搜索。
后来一次AC附上代码:
class Solution {
public:
int rows;
int cols;
int minnow;
int minPathSum(vector<vector<int> > &grid) {
// IMPORTANT: Please reset any member data you declared, as
……
哇这个题,把B数组的数合并到A数组中,AB原本有序,合并之后依然有序。
插入排序。
不过为什么自己写个插排写得这么死呢。
附上代码:
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
……
合并两个有序单向链表。
一次AC
附上代码:
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(l1 == NULL)
return l2;
if(l2 == NULL)
……
之前看编程珠玑的时候,记得先要有想法,就好像 啊哈!灵机一动。
然后要有相应的很好的数据结构。
最后编码。
这是我的一个理解。
然后这个题,真心呵呵。
自从本蒟蒻知道了二叉树序列化之后,就各种用。然后在这儿就用不停的Wa,190个测试数据
188个通过。
蛋疼。
憋了一天,忍不住了,看别人的想法,哎哟我去!这么点……
由于是二分查找树,根据根的不同来迭代。
附上代码。一次AC开始一直没想明白。哎。
class Solution {
public:
int numTrees(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n == 1)return 1;
if(n == 2)return 2;
if(n == ……
哎哟我擦。用递归让我AC了还不算我过,题目前面没打勾,对我这种有强迫症的人来说,好痛苦。回头想想别的办法。
class Solution {
public:
vector ans;
vector preorderTraversal(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance……
序列化二叉树。。。。
一次AC附上代码:
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vec……
这个题让我对二叉树的序列化有一个新的认识啦。
1
2 3
4 5 6
像这个树表示成:1 2 3 4 # 5 6
意义就是
1 | 2 3 | 4 # 5 6
分别是第一 第二 第三层。
这样的话只需要每次一层一层的枚举就好。
注意越界的问题,附上代码:
class Solution {
public:
void connect(TreeLinkNode *root……
两次二分,一次AC,注意越界的问题。
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test……
这个题,判断一个单链表是否存在闭环。不允许开辟额外的空间。
开始确实不知道怎么解,就去看别人的解法了。
大意是,两个指针,一个前进一步,另一个前进两步,如果到了NULL这两个指针还没出现相等的情况,欧克没有闭环,如果出现相等,就有闭环。
附上代码。个人觉得对指针和链表的操作我真心烂
class Solution {
publ……