class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int len=nums.size();
vector<int> res; for(int i=,temp=;i<len;i++)//把前面的乘起来,暂存起来
{
res.push_back(temp);
temp=temp*nums[i];
} for(int i=len-,temp=;i>=;i--)//倒着再乘一遍,刚好错开自己位置的数值
{
res[i]=res[i]*temp;
temp=temp*nums[i];
}
return res;
}
};

分析:

不给说O(n)时间复杂度,O(1)空间复杂度,我还真想不到这么好的。

leecode第二百三十八题(除自身以外数组的乘积)的更多相关文章

  1. leecode第二百三十六题(二叉树的最近公共祖先)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  2. leecode第二百三十五题(二叉搜索树的最近公共祖先)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  3. leecode第二百三十题(二叉搜索树中第K小的元素)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. leecode第一百三十六题(只出现一次的数字)

    class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...

  5. leecode第二百三十七题(删除链表中的节点)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  6. leecode第二百三十一题(2的幂)

    class Solution { public: bool isPowerOfTwo(int n) { bool is_flag=false; ) { ==)//如果为1,看是不是第一个1 { if( ...

  7. leecode第一百六十九题(求众数)

    class Solution { public: void quick_sort(vector<int>& nums,int res,int res_end) { )//错过,不能 ...

  8. 第二百三十四天 how can I 坚持

    今天果然不负众望,下了一天的雪啊,挺好. 今天把花搞了下,都弄花盆里了,希望不会就这么挂掉.八千代,绿萝,还有小叶元宝. 中午喝了点酒,没感觉. 过两天气温就零下十多度了,该咋办啊,最怕冬天.家里现在 ...

  9. leecode第一百五十五题(最小栈)

    class MinStack { public: stack<int> cur_stack; stack<int> cur_min;//用来存储最小值的栈 int min_nu ...

随机推荐

  1. SQL SERVER-时间戳(timestamp)与时间格式(datetime)互相转换

    SQL里面有个DATEADD的函数.时间戳就是一个从1970-01-01 08:00:00到时间的相隔的秒数.所以只要把这个时间戳加上1970-01-01 08:00:00这个时间就可以得到你想要的时 ...

  2. MongoDB系列----查

    开启查询: db.getMongo().setSlaveOk() 查版本: db.servion(); db.serverBuildInfo(); db.serverStatus().storageE ...

  3. 常用SQL语法

    1.替換A表中B字段中第二个字符 ,), 2.根据 Score 字段 排序 并分页 * from (select row_number() over(order by Score DESC) as r ...

  4. CentOS 7 安装samba服务

    STEP 1. 安装 #安装 [root@study ~]yum install smaba [root@study ~]systemctl start smb nmb STEP 2. 建立共享目录以 ...

  5. mutex,thread

    //#include <stdio.h> //#include <stdlib.h> //#include <unistd.h> #include <wind ...

  6. JavaScript中创建对象的几种模式

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. centos7安装gcc7.2.0

    1.有时候安装某些工具时,会提示gcc版本过低,需要安装更高版本. 进入/usr/src目录 cd usr/src 2.下载gcc7.2.0 wget 'http://mirrors-usa.go-p ...

  8. python & mysql 操作(compare_sum_fee)

    [1]源码 工作中涉及的python工具源码实现 费用比较工具源码: #!/usr/bin/python3 #coding = utf-8 import time, sqlite3, datetime ...

  9. BUAA Summer Practice 2017 #1 字符串专场

    https://vjudge.net/contest/262753#overview C - Regular Number HDU - 5972 bitset temp, temp[i]=1表示 此前 ...

  10. js 手动插入meta标签和script标签

    // 插入 meta 标签 var oMeta = document.createElement('meta'); oMeta.content = 'width=device-width, initi ...