【面试复习】leetcode算法题

【912】快排

class Solution {
public:
    void qsort(vector<int>& a, int L, int R) {
        if (L >= R) return;
        int l = L, r = R, pivot = a[l];
        while (l < r) {
            while (l < r && a[r] >= pivot) r--;
            while (l < r && a[l] <= pivot) l++;
            if (l < r) swap(a[l], a[r]); 
        }
        swap(a[l], a[L]);
        qsort(a, L, l-1);
        qsort(a, l+1, R);
    }

    vector<int> sortArray(vector<int>& nums) {
        qsort(nums, 0, nums.size() - 1);
        return nums;
    }
};

【206】链表反转

struct Solution {
	ListNode* reverseList(ListNode* head) {
		ListNode *prePtr = NULL, *curPtr = head;
		while (curPtr != NULL) {
			ListNode *nxtPtr = nxtPtr->next;

			curPtr->next = prePtr;
			prePtr = curPtr;
			curPtr = nxtPtr;
		}
		return prePtr;
	}
};

【剑22】链表倒k

class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        ListNode *fast = head, *slow = head;
        while (k--) fast = fast->next;
        while (fast) {
            fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
};

【】链表每K个进行反转

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* phd = new ListNode(-1);
        phd->next = head;
        ListNode *cur = head, *pre = phd;
        int len = 0;
        while (head) { len++; head = head->next; }

        for (int i = 0; i < len / k; i++) {
            for (int j = 0; j < k - 1; j++) {
                ListNode* nxt = cur->next;
                cur->next = nxt->next;
                nxt->next = pre->next;
                pre->next = nxt;
            }
            pre = cur;
            cur = pre->next;

        }
        return phd->next;
    }
};

【】线性第K大

// 线性第k大
class Solution {
public:
	int findKthLargest(vector<int> &nums, int k) {
		int result = 0;
		int numsSize = int(nums.size());
		if (numsSize == 0 || k > numsSize) {
			return 0;
		}
		int kMin = numsSize - k + 1;
        //cout << kMin << endl;
		result = select(nums, 0, numsSize - 1, kMin);
		return result;
	}

	int select(vector<int> &nums, int left, int right, int K) {
		if (left == right) 
			return nums[left];
		int cut = partition(nums, left, right);
		int currentResult = cut - left + 1;
		if (K == currentResult)
			return nums[cut];
		else if (K < currentResult)
			return select(nums, left, cut - 1, K);
		else
			return select(nums, cut + 1, right, K - currentResult);
		return -1;
	}

	int partition(vector<int> &nums, int left, int right) {
		int Pivot = nums[left];
		int i = left;
		int j = right;
		while (i < j) {
			while (nums[j] >= Pivot && i < j)j--;
			while (nums[i] <= Pivot && i < j)i++;
			if (i < j) swap(nums[i], nums[j]);
		}
		swap(nums[left], nums[i]);
		return i;
	}	
};

【】单调栈最大矩形

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        vector<int> stk(heights.size() + 1), L(heights.size()), R(heights.size());
        int top = 0;
        for (int i = 0; i < heights.size(); i++) {
            while (top && heights[stk[top]] >= heights[i]) top--;
            if (top) L[i] = stk[top] + 1;
            else L[i] = 0;
            stk[++top] = i; 
        }
        top = 0;
        for (int i = heights.size() - 1; i >= 0; i--) {
            while (top && heights[stk[top]] >= heights[i]) top--;
            if (top) R[i] = stk[top] - 1;
            else R[i] = heights.size() - 1;
            stk[++top] = i;
        }
        int ret = 0;
        for (int i = 0; i < heights.size(); i++) {
            // cout << L[i] << " " << R[i] << endl;
            ret = max(ret, heights[i] * (R[i] - L[i] + 1));
        }
        return ret;
    }
};

【】前中复原二叉树

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return buildTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
    }
    TreeNode* buildTree(vector<int>& preorder, int l1, int r1, vector<int>& inorder, int l2, int r2) {
        if (l1 > r1 || l2 > r2) return NULL;
        int val = preorder[l1];
        int roid = l2;

        for (int i = l2; i <= r2; ++i) {
            if (inorder[i] == val) {
                roid = i;
                break;
            }
        }

        TreeNode* root = new TreeNode(val);
        root->left  = 
            buildTree(preorder, l1 + 1, l1 + (roid - l2), inorder, l2, roid - 1);
        root->right = 
            buildTree(preorder, l1 + (roid - l2) + 1, r1, inorder, roid + 1, r2);
        return root;
    }
};

【】LRU实现

#define pa pair<int,int>
class LRUCache {
public:
    list<pa> L;
    unordered_map<int, list<pa> ::iterator> M;
    int capacity;
    LRUCache(int capacity) {
        L.clear();
        M.clear();
        this->capacity = capacity;
    }

    int get(int key) {
        auto it = M.find(key);
        if (it == M.end()) {
            return -1;
        }
        else {
            int val = it->second->second;
            L.erase(it->second);
            L.push_back({ key, val });
            M[key] = --L.end();
            return val;
        }
    }

    void put(int key, int value) {
        

        auto it = M.find(key);
        if (it != M.end()) L.erase(it->second);

        if (L.size() == capacity) {
            int k = L.front().first;
            M.erase(k);
            L.pop_front();
        }
        L.push_back({ key, value });
        M[key] = --L.end();
    }
};

【】链表环

class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fast = head, *slow = head;
        while (fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
            if (fast == slow) return 1;
        }
        return 0;
    }
};

【679】24点

class Solution {
public:
    bool judgePoint24(vector<int>& nums) {
        auto a = nums;
        sort(begin(a), end(a));
        do {
            auto a12 = cal(a[0], a[1]);
            // a12 op a34
            auto a34 = cal(a[2], a[3]);
            for (auto v1 : a12) {
                for (auto v2 : a34) {
                    auto res = cal(v1, v2);
                    for (auto r : res) {
                        if (is_ans(r)) return true;
                    }
                }
            }
            // a123 op a4
            for (auto v12 : a12) {
                auto a123 = cal(v12, a[2]);
                for (auto v123 : a123) {
                    auto res = cal(v123, a[3]);
                    for (auto r : res)
                        if (is_ans(r)) return true;
                }
            }
        } while (next_permutation(begin(a), end(a)));
        return false;
    }
    set<double> cal(double a, double b) {
        set<double> ans = {a * b, a + b, a - b, b - a};
        if (b) ans.insert(a / b);
        if (a) ans.insert(b / a);
        return ans;
    }
    bool is_ans(double a) {
        return abs(a - 24) < 1e-5;
    }
};

【628】三数乘积最大

排序后max(a[0]*a[1]*a[n-1], a[n-3]*a[n-2]*a[n-1])

【】一颗朴实无华的线段树

#include <bits/stdc++.h>
using namespace std;

#define lson rt << 1
#define rson rt << 1 | 1
#define mid ((l + r) >> 1)
#define Lson l, mid, lson
#define Rson mid + 1, r, rson
const int MAXN = 1e5 + 10;
int sum[MAXN << 2], add[MAXN << 2];
void pushup(int rt) {
    sum[rt] = sum[lson] + sum[rson];
}

void pushdown(int m, int rt) {
    if (add[rt]) {
        add[lson] += add[rt];
        add[rson] += add[rt];
        sum[lson] += add[rt] * (m - m / 2);
        sum[rson] += add[rt] * (m / 2);
        add[rt] = 0;
    }
}

void build(int l, int r, int rt) {
    add[rt] = 0;
    if (l == r) {
        sum[rt] = 0;
        return;
    }
    build(Lson), build(Rson);
    pushup(rt);
}

void update(int L, int R, int c, int l, int r, int rt) {
    if (L <= l && r <= R) {
        sum[rt] += c * (r - l + 1);
        add[rt] += c;
        return;
    }
    pushdown(r - l + 1, rt);
    if (L <= mid) update(L, R, c, Lson);
    if (R > mid) update(L, R, c, Rson);
    pushup(rt);
}

int query(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) {
        return sum[rt];
    }
    pushdown(r - l + 1, rt);
    int ret = 0;
    if (L <= mid) ret += query(L, R, Lson);
    if (R > mid) ret += query(L, R, Rson);
    return ret;
}
class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        vector<int> a(nums.size());
        
    }
};

int main() {
    build(1, 100, 1);
    int op, l, r, c;
    while (cin >> op >> l >> r) {
        if (op == 1) {
            cin >> c;
            update(l, r, c, 1, 100, 1);
        }
        else {
            cout << query(l, r, 1, 100, 1) << endl;
        }
    }
}

【】

在这里插入图片描述
在这里插入图片描述


#include <cuda_runtime.h>
#include "device_launch_parameters.h"

#include <stdio.h>
#include<stdlib.h>
#include<time.h>


//CPU版矩阵乘法
int matrixMutilCPU(float *A, float *B, int rowA, int colA, int rowB, int colB,float *C){
    float tmp ;
    if (colA != rowB)
        return -1;
    for (int i = 0; i < rowA; i++){
        for (int j = 0; j < colB; j++){ //loop for C
            tmp = 0;
            for (int k = 0; k < colA; k++){
                tmp += (A[colA*i+k]*B[colB*k+j]);
            }
            C[i*colB+j]= tmp;
        }
    }
    return 0;
}

//globle memory矩阵乘法
__global__ void matrixMutilGPU_slow(float *A, float *B, int colA, int colB, float *C){
    int xb = blockIdx.x;
    int yb = blockIdx.y;
    int x = threadIdx.x;
    int y = threadIdx.y;
    //计算结果矩阵的二维坐标
    int row = blockDim.y*yb + y;//blockDim.x == blockSize
    int col = blockDim.x*xb + x;
    float tsum = 0;
    //每一个block A和B的子块首地址
    for (int k = 0; k < colB; k++){
        tsum += (A[row*colA+k] * B[k*colB+col]);
    }
    //写入结果
    C[row*colB+col] = tsum;
}

// shared  memory 版分块矩阵乘法
template <int blockSize>
__global__ void matrixMutilGPU(float *A, float *B, int colA,int colB, float *C){
    int xb = blockIdx.x;
    int yb = blockIdx.y;
    int x = threadIdx.x;
    int y = threadIdx.y;

    //该线程负责的结果子块C,对应的A和B用于计算的起始子块
    //假设分成9个子块
    //  A11 A12 A13    B11 B12 B13
    //  A21 A22 A23  * B21 B22 B23 
    //  A31 A32 A33    B31 B32 B33  ,则计算C22时这样计算:A21*B12+A22*B22+A23*B32    
    float *BeginA = A + yb* blockSize*colA;
    float *EndA = BeginA + colA; 
    float *BeginB = B + blockSize*xb;
    int stepA = blockSize;
    int stepB = blockSize*colB;

    float tsum = 0;
    //每一个block A和B的子块首地址
    for (; BeginA < EndA; BeginA += stepA, BeginB += stepB){
        __shared__ float As[blockSize][blockSize];
        __shared__ float Bs[blockSize][blockSize];

        // 每个线程load一个元素到shared mem中
        As[y][x] = *(BeginA + y*colA + x);
        Bs[y][x] = *(BeginB + y*colB + x);
        __syncthreads();//同步
        for (int k = 0; k < blockSize;k++){
            tsum = tsum + As[y][k] * Bs[k][x];
        }
        __syncthreads();//同步,确保该块内所有线程都完成了计算。下次循环,要重复利用共享内存。
    }
    //写入结果 注意坐标的计算方法
    C[yb*blockSize*colB+y*colB+xb*blockSize+x]=tsum;
}

int main(int argc,char **argv){
    //1.construct Mat A & Mat B
    int rowA, colA,rowB,colB;
    rowA=1024;
    colA = 1024;
    rowB = 1024;
    colB = 1024;

    float *Ah, *Bh,*Ch;
    int memSizeA, memSizeB,memSizeC;
    memSizeA = sizeof(float)*colA*rowA;
    memSizeB = sizeof(float)*colB*rowB;
    memSizeC = sizeof(float)*colA*rowB;
    Ah = (float *)malloc(memSizeA);
    Bh = (float *)malloc(memSizeB);
    Ch = (float *)malloc(memSizeC);
    for (int k = 0; k < colA*rowA; k++)
        Ah[k] = 1.0l;
    for (int k = 0; k < colB*rowB; k++)
        Bh[k] = 0.1l;

    clock_t t1 = clock();
    matrixMutilCPU(Ah, Bh, rowA, colA, rowB, colB, Ch);
    clock_t t2 = clock();
    double time = (t2 - t1)*1.0 / CLOCKS_PER_SEC;
    printf("CPU cost:%.8lf s\n",time);
    printf("C[0]=%.8lf\n", Ch[0]);
    printf("=======================\n");
    for (int k = 0; k < colB*rowA; k++)
        Ch[k] = 0.0l;
    //2.set GPU params
    // By default, we use device 0, otherwise we override the device ID based on what is provided at the command line
    cudaSetDevice(0);
    const int blockSize = 16;
    cudaError_t error;
    dim3 threads(blockSize, blockSize);
    dim3 grid(colB / threads.x, rowA / threads.y);
    float *Ad, *Bd, *Cd;

    error=cudaMalloc(&Ad, memSizeA);
    if (error != cudaSuccess)
    {
        printf("cudaMemcpy (d_A,h_A) returned error %s (code %d), line(%d)\n", cudaGetErrorString(error), error, __LINE__);
        exit(EXIT_FAILURE);
    }

    error=cudaMalloc(&Bd, memSizeB);
    if (error != cudaSuccess)
    {
        printf("cudaMemcpy (d_A,h_A) returned error %s (code %d), line(%d)\n", cudaGetErrorString(error), error, __LINE__);
        exit(EXIT_FAILURE);
    }
    error=cudaMalloc(&Cd, memSizeC);
    if (error != cudaSuccess)
    {
        printf("cudaMemcpy (d_A,h_A) returned error %s (code %d), line(%d)\n", cudaGetErrorString(error), error, __LINE__);
        exit(EXIT_FAILURE);
    }

    //3. move data from host to device
    cudaMemcpy(Ad, Ah, memSizeA, cudaMemcpyHostToDevice);
    cudaMemcpy(Bd, Bh, memSizeB, cudaMemcpyHostToDevice);
    cudaMemcpy(Cd, Ch, memSizeC, cudaMemcpyHostToDevice);

    //4.compute
    t1 = clock();
    //matrixMutilGPU_slow << <grid, threads >> >(Ad, Bd, colA, colB, Cd); //全局显存
    matrixMutilGPU<blockSize> << <grid, threads >> >(Ad, Bd, colA, colB, Cd);//共享内存

    //5.move data to host
    cudaMemcpy(Ch, Cd, memSizeC, cudaMemcpyDeviceToHost);
    t2 = clock(); //计算GPU耗时 包括内存移动
    time = (t2 - t1)*1.0 / CLOCKS_PER_SEC;
    printf("GPU cost:%.8lf s\n", time);
    printf("C[0]=%.8lf\n", Ch[0]);

    //6.release memory
    free(Ah);
    free(Bh);
    free(Ch);
    cudaFree(Ad);
    cudaFree(Bd);
    cudaFree(Cd);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值