随笔分类 - 题库Leetcode简单题
https://leetcode.com/problemset/algorithms/中Easy级别的题
摘要:给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如, _______6______ / \ ___2__ ___8__ / \ / \ 0 4 7 9 / \ 3 52和8的最近公共祖先是6,2和4的最近公共祖先是2,假设找的3和5 TreeNode* l =lowestCommonAnces
阅读全文
摘要:二叉树的中序遍历,即左子树,根, 右子树 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int
阅读全文
摘要:二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; ...
阅读全文
摘要:统计一个值的二进制1的个数,用与(&)和向左移位(>1){ 6 ans += n&1; 7 } 8 return ans; 9 }10 };解法二: 1 class Solution { 2 public: 3 int hamm...
阅读全文
摘要:字符串s和字符串t是否异构,就是统计两个字符串的a-z的字符数量是否一值 1 class Solution { 2 public: 3 4 bool isAnagram(string s, string t) { 5 int flgs[26] = {0};//统计s a-z...
阅读全文
摘要:找出数组中重复的数,裸的map和set题 1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 map<int,int> mii; 5 for (int i = 0;i<nums.size() ;++
阅读全文
摘要:本质是把26进制转化为10进制 A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 1 class Solution { 2 public: 3 int titleToNumber(string s...
阅读全文
摘要:计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 1 class Solution { 2 public: 3 int maxDepth(TreeNode* root) { 4 if(!root) return 0; 5 else return max(maxDepth(ro
阅读全文
摘要:就是判断两棵树的值和结构是否相同注意:要判断是否所有的树节点是否为NULL 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; ...
阅读全文
摘要:将链表节点序号(不是值)是偶数的放到链表后面, 如Given1->2->3->4->5->NULL,return1->3->5->2->4->NULL.我首先统计了下链表的大小cnt,同时求出链表尾端end,然后直接将每个链表节点序号是奇数的点后面的节点放到end后面去,同时更新end,这样更新cn...
阅读全文
摘要:交换左右叶子节点 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ...
阅读全文
摘要:如题 删除是要注意让现在的链表等于下一个链表的值1 class Solution {2 public:3 void deleteNode(ListNode* node) {4 ListNode *nextnode = node ->next;5 *node =...
阅读全文
摘要:1 class Solution {2 public:3 int addDigits(int num) {4 if(num == 0 ) return num;5 else return num % 9 == 0 ? 9 : num % 9;6 }7 ...
阅读全文
摘要:class Solution {public: bool canWinNim(int n) { return n % 4 != 0; }};
阅读全文
摘要:1 class Solution { 2 public: 3 void moveZeroes(vector& nums) { 4 int j = 0; 5 for(int i = 0; i< nums.size(); ++i){ 6 ...
阅读全文
摘要:今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表。在程序中具体表现为 one = change(one); //一倍速度 two = change(
阅读全文