https://github.com/Hana-ame/leetcode

开始写leetcode了,但是想了想好像没什么好写的

比如链表反转要不能成环,否则bug,好像也太水了吧。

比如前面一题还是用vector转换成array才终于不超时的。
但是今天一题就是vector的索引比array还快,不科学。

// https://leetcode.com/problems/linked-list-random-node/
class Solution {
public:
    int len;
    // int arr[10001];
    vector<int> nums;    
    // int* arr;
    Solution(ListNode* head) {
        int n;
        ListNode* ptr = head;
        // int arr[10001];
        for (n=0; ptr != NULL; n++){
            // arr[n] = ptr->val;
            nums.push_back(ptr->val);
            ptr = ptr->next;
            if (ptr == NULL) break;
        }
        len = ++n;
        // arr = &nums[0];
    }
    
    int getRandom() {
        int i = rand() % len;
        // return arr[i];
        return nums[i];
    }
};

另外还有java这么快到底是怎么做到的,比cpp快多了。
说是内存换时间不过cpp要怎么做才能内存换时间嘞,不懂。