摘要:1 /* 2 * clockwise rotate 3 * first reverse up to down, then swap the symmetry 4 * 1 2 3 7 8 9 7 4 1 5 * 4 5 6 => 4 5 6 => 8 5 2 6 * ...
阅读全文
摘要:Given an array of numbersnums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements t...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu...
阅读全文
摘要:题目链接Problem discription:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Accepted Code: 1 /** 2 * Def...
阅读全文
摘要:题目链接Problem discriptionGiven a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets mus...
阅读全文
摘要:题目链接Problem discriptionGiven two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size t...
阅读全文
摘要:题目链接Problem description:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would you...
阅读全文
摘要:题目链接Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next po...
阅读全文
摘要:题目链接题意:给出长度为n的数组,和整数elem, 要求删除数组中存在的elem,返回最终的数组长度。附上代码: 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 //...
阅读全文
摘要:题目链接题意:实现pos(x, n)功能, 其中x为double类型,n为int类型附上代码: 1 class Solution { 2 public: 3 double pow(double x, int n) { 4 // handle several particula...
阅读全文
摘要:题目链接题意: 给出 n * n的矩阵,要求将矩阵顺时针旋转90°(不使用额外空间) 1 /* 2 Basically, divide the array into 4 along the diagonals, 3 then for each element in the top ...
阅读全文
摘要:题目链接题意: 给定一棵二叉树, 判断是否为平衡二叉树, 这里的平衡二叉树指的是:每个结点的左右子树的深度之差不超过1。附上代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ...
阅读全文
摘要:题目链接求二叉树最小深度,最小深度指的是:从根节点走到最近的叶子结点的最短长度附上代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6...
阅读全文
摘要:题目链接判断一颗二叉树是否是二叉搜索树(二叉排序树),也就是BST如果该二叉树是BST, 那么对其中序遍历,所得序列一定是单调递增的(不考虑有重复数值的情况)附上代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 *...
阅读全文
摘要:题目链接将罗马数字转换为十进制整数。数据范围为:1~3999罗马数字由7种字母组成,按照特定规则可以组成任意正整数, 需要注意的是罗马数字中没有”0“.关于罗马数字具体可以看:这里附上代码: 1 class Solution { 2 public: 3 int romanToInt(stri...
阅读全文
摘要:题目链接求树的最大深度附上代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ...
阅读全文
摘要:题目链接简单倒推附上代码: 1 class Solution { 2 public: 3 int minPathSum(vector > &grid) { 4 int n = (int)grid.size(); 5 if (n == 0) return 0; ...
阅读全文
摘要:题目链接又是一个考察对链表基本操作的题目附上代码: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * Lis...
阅读全文
摘要:题目链接判断字符串是否为回文串。附上代码: 1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 if (s.empty()) return true; // consider empty string ...
阅读全文
摘要:题目链接对unordered_set和set的内部实现还不了解,只知道前者是hash实现的,后者是用R-B-Tree实现。有时间需要读下源码。附上代码: 1 class Solution { 2 public: 3 int longestConsecutive(vector &num) { ...
阅读全文