头像

小宇宙_8




离线:2个月前


最近来访(4)
用户头像
克里斯保罗
用户头像
繁花似锦
用户头像
CYD1
用户头像
minnn

活动打卡代码 AcWing 862. 三元组排序

小宇宙_8
2021-11-30 13:11
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>

#define x first
#define y second

using namespace std;

const int N = 10010;

typedef pair<int, pair<double, string >> PII;

vector<PII> ans;
int n, a;
double b;
string s;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ )
    {
        cin >> a >> b >> s;
        ans.push_back({a, {b, s}});
    }

    sort(ans.begin(), ans.end());

    for (auto i: ans)
        printf("%d %.2lf %s\n",i.x, i.y.x, i.y.y.c_str());   

    return 0;
}







小宇宙_8
2021-11-30 13:09
class Solution {
public:
    int NumberOf1(int n) {
        int res = 0;
        unsigned int un = n; 
        while (un) res += un & 1, un >>= 1;
        return res;
    }
};




活动打卡代码 AcWing 51. 数字排列

小宇宙_8
2021-11-30 13:08

[//]: # 抄个答案


class Solution {
public:
    vector<bool> st;
    vector<int> path;
    vector<vector<int>> ans;


    vector<vector<int>> permutation(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        st = vector<bool>(nums.size(), false);
        path = vector<int>(nums.size());
        dfs(nums, 0, 0);
        return ans;
    }

    void dfs(vector<int>& nums, int u, int start)
    {
        if (u == nums.size())
        {
            ans.push_back(path);
            return;
        }

        for (int i = start; i < nums.size(); i ++ )
            if (!st[i])
            {
                st[i] = true;
                path[i] = nums[u];
                if (u + 1 < nums.size() && nums[u + 1] != nums[u])
                    dfs(nums, u + 1, 0);
                else
                    dfs(nums, u + 1, i + 1);
                st[i] = false;
            }
    }

};





小宇宙_8
2021-11-30 11:49
class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
      unordered_set<int> us;
      for(int a:nums)
      {
        if(us.count(target-a))
          return vector<int>({target-a,a});
        us.insert(a);
      }
    }
};



活动打卡代码 AcWing 53. 最小的k个数

小宇宙_8
2021-11-30 11:46
class Solution {
public:
    vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
      sort(input.begin(),input.end());
      return vector<int>(input.begin(),input.begin()+k);
    }
};





小宇宙_8
2021-11-30 11:40

```

class MyQueue {
public:
/* Initialize your data structure here. /
stack[HTML_REMOVED] stk, cache;

MyQueue() {

}

/** Push element x to the back of queue. */
void push(int x) {
    stk.push(x);
}

void copy(stack<int> &a, stack<int> &b) {
    while (a.size()) {
        b.push(a.top());
        a.pop();
    }
}

/** Removes the element from in front of queue and returns that element. */
int pop() {
    copy(stk, cache);
    int res = cache.top();
    cache.pop();
    copy(cache, stk);
    return res;
}

/** Get the front element. */
int peek() {
    copy(stk, cache);
    int res = cache.top();
    copy(cache, stk);
    return res;
}

/** Returns whether the queue is empty. */
bool empty() {
    return stk.empty();
}

};

/*
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
/

```




小宇宙_8
2021-11-30 11:26
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> printListReversingly(ListNode* head) {
        vector<int> res;
        while (head) {
            res.push_back(head->val);
            head = head->next;
        }
        return vector<int>(res.rbegin(), res.rend());
    }
};




小宇宙_8
2021-11-30 11:21
class Solution {
public:
    void reOrderArray(vector<int> &array) {
         int l = 0, r = array.size() - 1;
         while (l < r) {
             while (l < r && array[l] % 2 == 1) l ++ ;
             while (l < r && array[r] % 2 == 0) r -- ;
             if (l < r) swap(array[l], array[r]);
         }
    }
};




小宇宙_8
2021-11-30 11:20
class Solution {
public:
    int getMissingNumber(vector<int>& nums) {
        int n=0;
        for(int x:nums)
        {
            if(n!=x)
            break;
            n++;
        }
        return n;   
    }
};




小宇宙_8
2021-11-30 11:18
class Solution {
public:
    int getNumberOfK(vector<int>& nums , int k) {
        int cnt = 0;
        for(int x : nums)
            if(x == k)
                cnt++;
        return cnt;
    }
};