Giter Site home page Giter Site logo

leetcode's Introduction

leetcode 解题算法

主要是使用 JavaScript 语言来解题,部分采用的是 C/C++。

这个仓库是具体的解题答案,若想了解一些数据结构,可以查看:wenzi0github / algorithm-by-javascript

leetcode's People

Contributors

wenzi0github avatar

Stargazers

 avatar

Watchers

 avatar  avatar

leetcode's Issues

数组的所有子集

获取所有的子集有多种方式。

前提,数组中所有的数据均不相同。

https://leetcode.cn/problems/subsets/

1. 从结果中取出每一项添加 nums[i]

class Solution {
public:
  vector<vector<int>> subsets(vector<int>& nums) {
    vector<int> temp;
    vector<vector<int>> result;
    int i, j, size, resSize;
    size = (int)nums.size();

    result.push_back(temp);

    for(i=0; i<size; i++)
    {
      for(j=0, resSize=(int)result.size(); j<resSize; j++)
      {
        vector<int> temp1 = result[j];
        temp1.push_back(nums[i]);
        result.push_back(temp1);
      }
    }


    return result;
  }
};

2. 递归对每个数字进行「使用」和「不使用」两种选择

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
      vector<int> temp;
      vector<vector<int>> result;

      find(nums, 0, temp, result);

      return result;
    }
private:
  int find(vector<int>& nums, int curIndex, vector<int>& temp, vector<vector<int>>& result)
  {
    if (curIndex >= nums.size())
    {
      result.push_back(temp);
      return 0;
    }
    // 每个当前的数字,都有使用和不使用两种选择
    // 不使用该数字
    find(nums, curIndex+1, temp, result);

    // 使用该数字
    vector<int> needTemp = temp;
    needTemp.push_back(nums[curIndex]);
    find(nums, curIndex+1, needTemp, result);
    return 0;
  }
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.