查看: 1441| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Leetcode 78. Subsets的followup

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
subsets的followup
https://leetcode.com/problems/subsets/
memory不够怎么办?

请个位大佬给个思路,谢谢。

评分

参与人数 2大米 +4 收起 理由
jasoninfoshare + 1 给你点个赞!
14417335 + 3

查看全部评分


上一篇:请教一道问题,C语言相关
下一篇:请问大家关于@functools.lru_cache(none)的问题
推荐
twtypsj 2021-3-24 13:03:01 | 只看该作者
全局:
本帖最后由 twtypsj 于 2021-3-24 13:04 编辑

大概写了一下,代码长这样:



  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6. void add(string in, string out, int n)
  7. {
  8.     fstream infile(in, ios::in);
  9.     fstream outfile(out, ios::out);
  10.     string line;
  11.     while(getline(infile, line))
  12.         outfile<<line<<" "<<to_string(n)<<endl;
  13.     infile.close();
  14.     outfile.close();
  15. }
  16. void append(string in, string out)
  17. {
  18.     fstream infile(in, ios::in);
  19.     fstream outfile(out, ios::out|ios::app);
  20.     string line;
  21.     while(getline(infile, line))
  22.         outfile << line << endl;
  23.     infile.close();
  24.     outfile.close();
  25. }
  26. void subset(vector<int> nums, string filename)
  27. {
  28.     string tmp = "tmp.txt";
  29.     fstream file;
  30.     file.open(filename,ios::out);
  31.     file<<endl;
  32.     file.close();

  33.     for(int n : nums)
  34.     {
  35.         add(filename, tmp, n);
  36.         append(tmp, filename);
  37.     }
  38. }
  39. int main()
  40. {
  41.     string filename = "subset.txt";
  42.     subset({1,2,3,4}, filename);
  43.     fstream res(filename, ios::in);
  44.     string line;
  45.     while(getline(res, line)) cout << line << endl;
  46.     return 0;
  47. }





复制代码


对应的输出是:
                                                                                                                                         
1                                                                                                                                       
2                                                                                                                                       
1 2                                                                                                                                    
3                                                                                                                                       
1 3                                                                                                                                    
2 3                                                                                                                                    
1 2 3                                                                                                                                   
4                                                                                                                                       
1 4                                                                                                                                    
2 4                                                                                                                                    
1 2 4                                                                                                                                   
3 4                                                                                                                                    
1 3 4                                                                                                                                   
2 3 4                                                                                                                                   
1 2 3 4                                                                                                                                 
                                                                                                                                         
                                                                                                                                         
...Program finished with exit code 0                                                                                                     
Press ENTER to exit console.

评分

参与人数 2大米 +7 收起 理由
14417335 + 6
blackrose + 1 赞一个

查看全部评分

回复

使用道具 举报

🔗
twtypsj 2021-3-24 11:36:55 | 只看该作者
全局:
本帖最后由 twtypsj 于 2021-3-24 11:43 编辑

内存不够的话,就用磁盘
这是齐士巴的代码,可以把res想象成一个文件,vector<int> tmp = res[i]可以认为是从文件中读取一行数据,res.push_back(tmp)可以认为是向文件追加一行数据。


  1. class Solution {
  2. public:
  3.     vector<vector<int>> subsets(vector<int>& nums) {
  4.         vector<vector<int>> res{{}};
  5.         for(int n : nums)
  6.         {
  7.             int size = res.size();
  8.             for(int i=0; i<size; ++i)
  9.             {
  10.                 vector<int> tmp = res[i];
  11.                 tmp.push_back(n);
  12.                 res.push_back(tmp);
  13.             }
  14.         }
  15.         return res;
  16.     }
  17. };
复制代码


评分

参与人数 2大米 +4 收起 理由
14417335 + 3
blackrose + 1 赞一个

查看全部评分

回复

使用道具 举报

🔗
 楼主| andysli6590 2021-3-25 03:51:58 | 只看该作者
全局:
用iteration写就行:

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums == null || nums.length == 0){
            return res;
        }
        
        List<Integer> sub = new ArrayList<>();
        res.add(sub);
        
        for(int i = 0; i < nums.length; i++){
            final int size = res.size();
            for(int j = 0; j < size; j++){
                List<Integer> temp = new ArrayList<>(res.get(j));
                temp.add(nums);
                res.add(temp);
            }
        }
        return res;
    }
}
回复

使用道具 举报

🔗
twtypsj 2021-3-25 07:00:28 | 只看该作者
全局:
本帖最后由 twtypsj 于 2021-3-25 07:06 编辑
andysli6590 发表于 2021-3-25 03:51
用iteration写就行:

class Solution {

这就不太懂内存不够的意思了,如果输入是1M个数字,输出是2的1M次方大小吧,也就是1EB?
不知道内存不够是不是指这个最终保存结果的内存不够?

评分

参与人数 1大米 +1 收起 理由
blackrose + 1 赞一个

查看全部评分

回复

使用道具 举报

🔗
twtypsj 2021-3-25 07:19:46 | 只看该作者
全局:
不用临时文件的版本



  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6. void subset(vector<int> nums, string filename)
  7. {
  8.     fstream file(filename, ios::out);
  9.     file<<endl;
  10.     file.close();
  11.     int count = 1;
  12.     for(int n : nums)
  13.     {
  14.         fstream in(filename, ios::in);
  15.         fstream out(filename, ios::out|ios::app);
  16.         string line;
  17.         for(int i=0;i<count&&getline(in, line);++i)
  18.         {
  19.             out<<line<<" "<<to_string(n)<<endl;
  20.         }
  21.         count = count << 1;
  22.         in.close();
  23.         out.close();
  24.     }
  25. }
  26. int main()
  27. {
  28.     string filename = "subset.txt";
  29.     subset({1,2,3,4}, filename);
  30.     fstream file(filename, ios::in);
  31.     string line;
  32.     while(getline(file, line)) cout << line << endl;

  33.     return 0;
  34. }
复制代码

评分

参与人数 1大米 +1 收起 理由
blackrose + 1 赞一个

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表