leetcode随笔
来源:互联网 发布:狙击手设置准星软件 编辑:程序博客网 时间:2024/06/06 10:42
leetcode随笔杂谈
leetcode9道简单题目
1.Given an integer, write a function to determine if it is a power of two.
知识点:二进制的移位操作;能被2的n次幂数用二进制表示只有一个1
方法:
I.①将数字(n=1)与原数字进行与(&)操作②同时进行左移位(<<1),③求出1的出现的个数。
II.原数字&(原数字-1),如果为2的倍数则结果为0(方法很巧妙)
2.Given an integer, write a function to determine if it is a power of three.
知识点:数学对数换底公式;float的差值是1e-10,其并没有精确值
方法:
将加上abs((int)(i+0.5)-i)进行强制int类型转换,与i值作比较差值小于1e-10则在是其幂次方。(i=log3(n)其对数形式)
3.Happy number
知识点:hashTable应用(c++ set只存储键值)
方法:
①将happy number的和存入set集合.②find set集合当中是否存在sum值(如果存在表明陷入到某个数的loop当中,说明不是happynumber;如果==1则返回true)
4.Ugly Number
知识点:数字找规律
方法:①判断输入值(n)是否能被num=2,3,5整除.②如果能,则updata n=n/num继续判断(终止条件为n是否小于等于7)。
5.Implement Queue using Stacks
知识点:栈的基本概念及操作;队列的基本概念及操作
方法:
I.一个栈作为辅助存储空间,并且始终保持为空。
II.一个栈作为入栈栈,另一个作为出栈栈。
因此知识点很重要,所以代码附到下面:
class Queue {public: // Push element x to the back of queue. stack<int> stoSta1,stoSta2; void push(int x) { if(stoSta2.empty()) stoSta1.push(x); else { while(stoSta2.size()>0) { int temp=stoSta2.top(); stoSta1.push(temp); stoSta2.pop(); } stoSta1.push(x); } } // Removes the element from in front of queue. void pop(void) { if(stoSta2.size()>0) { stoSta2.pop(); } else { while(stoSta1.size()>0) { int temp=stoSta1.top(); stoSta2.push(temp); stoSta1.pop(); } stoSta2.pop(); } } // Get the front element. int peek(void) { if(stoSta2.size()>0) { return stoSta2.top(); } else { while(stoSta1.size()>0) { int temp=stoSta1.top(); stoSta2.push(temp); stoSta1.pop(); } return stoSta2.top(); } } // Return whether the queue is empty. bool empty(void) { if(stoSta1.size()==0&&stoSta2.size()==0) return true; else return false; }};
思路补充:出栈操作保证一个栈为空,入栈操作保证在队1和对2当中交替进行
6.Balanced Binary Tree
知识点:平衡二叉树的左右子树均为平横二叉树;二叉树的深度
方法:
①递归求解二叉树的深度②递归判断二叉树是否为平横二叉树
代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isBalanced(TreeNode* root) { if(!root) { return true; } int l=pathLengthHeight(root->left); int r=pathLengthHeight(root->right); if(l-r>1||r-l>1) { return false; } return isBalanced(root->left)&&isBalanced(root->right); } int pathLengthHeight(TreeNode*root) { if(!root) { return 0; } int max1=pathLengthHeight(root->left)+1; int max2=pathLengthHeight(root->right)+1; return (max1>max2)?max1:max2; }};
python 代码:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True if abs(self.depth(root.left)-self.depth(root.right))<=1: return self.isBalanced(root.left) and self.isBalanced(root.right) else: return False def depth(self,root): if not root: return 0 if root and not root.left and not root.right: return 1 return max(self.depth(root.left)+1,self.depth(root.right)+1)
7.Lowest Common Ancestor of a Binary Search Tree
知识点:二叉排序树的中序便利是有序数组;二叉排序树最低公共祖先的范围[min,max]之间
方法:递归判断二叉树的值是否在[min,max]之间
代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { int max; int min; if(p->val>q->val) { max=p->val; min=q->val; } else { max=q->val; min=p->val; } if(root->val<=max&&root->val>=min) { return root; } if(root->val<min) { return lowestCommonAncestor(root->right,p,q); } if(root->val>max) { return lowestCommonAncestor(root->left,p,q); } }};
python 代码:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def lowestCommonAncestor(self, root, p, q): """ :type root: TreeNode :type p: TreeNode :type q: TreeNode :rtype: TreeNode """ if(p.val>q.val): temp=p p=q q=temp if root.val>q.val: return self.lowestCommonAncestor(root.left,p,q) if root.val<p.val: return self.lowestCommonAncestor(root.right,p,q) return root
8.Climbing Stairs
知识点:动态规划(以后集中讲解);数学找递归公式;递归与非递归的转换
方法:
①第一次爬一个阶梯则下面有f(n-1)种爬法; ②第一次爬两个阶梯则下面有f(n-2)中爬法③递推规律为:f(n)=f(n-1)+f(n-2);
代码如下:
class Solution {public: int climbStairs(int n) { /* if(n==1) { return 1; } if(n==2) { return 2; } return climbStairs(n-1)+climbStairs(n-2); } */ vector<int> sta; sta.push_back(1); sta.push_back(2); for(int i=2;i<n;i++) { int temp=sta[i-1]+sta[i-2]; sta.push_back(temp); } return sta[n-1]; }};
9.Roman to Integer
知识点:数学找Roman的规律;HashTable的应用
方法:
①将Roman数字表示的十进制数用HashTable存储(c++ map)②从右向左依次比较Roman数字的十进制值(小则加,大则减)③求出总体的和
代码如下:
class Solution {public: int romanToInt(string s) { map<char,int> hashTable; hashTable.insert(make_pair('I',1)); hashTable.insert(make_pair('V',5)); hashTable.insert(make_pair('X',10)); hashTable.insert(make_pair('L',50)); hashTable.insert(make_pair('C',100)); hashTable.insert(make_pair('D',500)); hashTable.insert(make_pair('M',1000)); int sum = hashTable.find(s[s.size() - 1])->second; for (int i = s.size() - 1; i > 0; i--) { if (hashTable.find(s[i])->second <= hashTable.find(s[i - 1])->second) { sum += hashTable.find(s[i - 1])->second; } else { sum -= hashTable.find(s[i - 1])->second; } } return sum; }};
总结
综上,I.我们需要不断储备基础知识:①找数字当中存在的规律②熟悉基本数据结构概念③掌握组成原理里面移位等操作;II.让我们我们一同努力,明天会更好!
- leetcode随笔
- Leetcode 随笔
- leetcode随笔II
- leetcode随笔III
- leetcode随笔IV
- leetcode随笔V
- leetcode随笔VI
- leetcode随笔VII
- leetcode随笔VIII
- Leetcode闲逛随笔
- LeetCode随笔之Array
- LeetCode随笔之栈
- LeetCode随笔之backtracking
- LeetCode随笔之Dynamic programming
- LeetCode随笔之链表
- LeetCode随笔之排序算法
- LeetCode随笔之Binary Tree(二叉树)
- LeetCode随笔之BFS广度优先搜索
- 算法——并查集
- php 打印数组千万不能放在字符串中
- 毛玻璃效果 BLurImage
- [Lintcode]Remove Duplicates from Sorted List
- PHP实现zip压缩和解压缩操作
- leetcode随笔
- linux学习笔记三_umask
- WeUI 入门教程
- C++笔试题 String类的实现 三大复制控制函数
- 非静态内部类为什么可以访问外部类的静态成员
- 51nod 1444 破坏道路
- Leetcode 299:Bulls and Cows
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- Integer和int的区别