leetcode学习笔记3
来源:互联网 发布:精雕用什么软件编程好 编辑:程序博客网 时间:2024/05/29 19:03
Factorial Trailing Zeroes
Given an integer n,return the number of trailing zeroes in n!.
Note: Yoursolution should be in logarithmic time complexity.
class Solution {
public:
int trailingZeroes(intn) {
int ret = 0;
while(n)
{
ret += n/5;
n /= 5;
}
return ret;
}
};
注:首先分析N!末尾的0,只有2*5的情况下才会出现。所以对N!进行素数分解:N!=2^i****5^j***.
则末尾0的个数为min(i,j). 又由于i明显远大于j,所以我们只要求解j即可。
又j均由1-N中5的倍数所提供,如果该数仅为5的倍数则贡献1,若为25的倍数则贡献2.
而1-N中,5的倍数个数为|_N/5_|,|_ _|表示取下整数。
又1-N中,25的倍数个数为|_N/25_|.需要注意到的是|_N/5_|已经包含了|_N/25_|。又25的倍数贡献了2,所以考虑到避免重复计算,则N!的素数分解中5的指数j为:
j = |_N/5_|+|_N/25_|+|_N/5^3_|+...
Pascal'sTriangle
MySubmissions
Question
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
if(numRows==0) return res;
for(int i=0;i<numRows;i++){
vector<int> temp;
for(int j=0;j<=i;j++){
if(j==0||j==i)
temp.push_back(1);
elsetemp.push_back(res[i-1][j-1]+res[i-1][j]);
}
res.push_back(temp);
}
return res;
}
};
Remove Duplicates from Sorted Array
Given a sorted array, removethe duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do thisin place with constant memory.
For example,
Given input array nums = [1,1,2]
,
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==0) return NULL;
if(nums.size()==1) return 1;
int count=1,r=nums[0];
for(int i=0;i<nums.size();i++){
if(nums[i]!=r)
nums[count++]=nums[i];
r=nums[i];
}
return count;
}
};
Remove Element
Given an array and a value,remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what youleave beyond the new length.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int count=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=val){
nums[count]=nums[i];
count++;
}
}
return count;
}
};
Binary Tree Level Order Traversal II
Given a binary tree, returnthe bottom-uplevel order traversal of itsnodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
stack<vector<int>> stack;
int count;
TreeNode* temp;
if(root==NULL) return res;
q.push(root);
while(q.size()>0){
count=q.size();
vector<int> q1;
while(count--){
temp=q.front();
q1.push_back(temp->val);
if(temp->left)q.push(temp->left);
if(temp->right)q.push(temp->right);
q.pop();
}
stack.push(q1);
}
while(stack.size()>0){
res.push_back(stack.top());
stack.pop();
}
return res;
}
};
- leetcode学习笔记3
- LeetCode学习笔记
- leetcode学习笔记1
- leetcode学习笔记2
- leetcode学习笔记4
- leetcode学习笔记5
- Leetcode学习笔记
- leetcode学习笔记
- LeetCode学习笔记-Day1
- LeetCode学习笔记-Day2
- leetcode学习笔记:Combinations & Subsets
- 【leetcode学习笔记】leecode是什么?
- LeetCode学习笔记[3]:Longest Substring Without Repeating Characters
- leetcode笔记:3Sum
- leetcode学习笔记:Trapping Rain Water
- LeetCode 学习笔记:Question 1、Two Sum
- 算法学习笔记[LeetCode]Valid Parentheses
- 算法学习笔记[LeetCode]Min Stack
- UIGestureRecognizer-手势识别器
- Cocos2d-x shader学习1: 灰度shader
- MongoDB 初见指南
- 无缝隙滚动
- Oracle学习(序言)
- leetcode学习笔记3
- 从 0 到 1:微信后台系统的演进之路
- rinetd安装
- java接口和抽象类的区别和作用(功能、用途、好处)
- 计算机网络原理笔记(三、数据链路层)
- NYOJ:游戏高手的烦恼【二分图——最小点覆盖数】
- uva11078- Open Credit System
- HTML string对象的操作
- 安全测试之使用含有已知漏洞的组件