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;

       

    }

};

0 0
原创粉丝点击