虾皮 shopee 笔试 代码

第一题ac 找出不在数组内的最小正数
class Solution {
public:
    /**
     *
     * @param nums int整型一维数组 入参数组
     * @param numsLen int nums数组长度
     * @return int整型
     */
    int firstMissingPositive(int* nums, int numsLen) {
        //我们要找的这个最小的数一定属于[1,numslen+1]
        set<int> s;
        for(int i=0; i<numsLen; i++){
            if( s.find(nums[i])==s.end() ){
                s.insert(nums[i]);
            }
        }
        for(int i=1; i<=numsLen+1; i++){
            if( s.find(i)==s.end() ){
                return i;
            }
        }
        
    }
};

第二题ac 判断是否为回文字符串,输入为单向链表,明确要求时间复杂度O(n),空间复杂度O(1)
/**
 * struct ListNode {
 *    int val;
 *    struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     *
     * @param pHead ListNode类
     * @return bool布尔型
     */
    
    
    bool isPalindrome(ListNode* pHead) {
        //后序代码中的链表长度至少为2
        if( pHead==NULL || pHead->next==NULL ){
            return true;
        }
        //将链表前半部分反转,相当于从中间劈开:需要遍历2遍,第一遍用来记录链表个数,第二遍来反转前半部分
        //然后再逐个结点比较即可。
        //链表长度
        int count = 0;
        ListNode* temp = pHead;
        while( temp!=NULL ){
            count++;
            temp = temp->next;
        }
        //反转前半部分(pHead),后半部分只需要不断右移即可(qHead)
        ListNode* qHead = pHead->next;
        pHead->next = NULL;
        for(int i=0; i<count/2-1; i++){
            //后半部分右移
            temp = qHead;
            qHead = qHead->next;
            //前半部分新加入一个头部
            temp->next = pHead;
            pHead = temp;
        }
        //注意如果结点个数为奇数,后半部分在比较之前,头部要后移一位
        if( count%2 ){
            qHead = qHead->next;
        }
        //开始逐个比较
        for(int i=0; i<count/2; i++){
            if( pHead->val != qHead->val ){
                return false;
            }
            pHead = pHead->next;
            qHead = qHead->next;
        }
        return true;
        
    }
};

第三题暴力解ac 输出无效交易。无效的两种情况:1、金额超出1000;2、同一名称不同城市的交易时间差<=60。注意输入的字符串要做以','的分割。
class Solution {
public:
    /**
     *
     * @param vTransaction string字符串vector
     * @return string字符串vector
     */
    vector<string> invalidTransaction(vector<string>& vTransaction) {
        vector<string> anws;
        int size = vTransaction.size();
        if(size==0){
            return anws;
        }
        int flag[size];
        for(int i=0; i<size; i++)    flag[i] = 0;
        
        //先分割输入
        vector<vector<string> > words; //0姓名,1时间,2金额,3城市
        for(int i=0; i<size; i++){
            words.push_back( splitTrans(vTransaction[i]) );
        }
        //遍历过程中,每次都去和后面的交易做比较,一旦有判定无效的就标记1
        for(int i=0; i<size; i++){
            vector<string> trans = words[i];//0姓名,1时间,2金额,3城市
            //已经被判定无效了,就不用管
            if( flag[i]==1 )
                continue;
            //金额超出1000
            if(stoi(trans[2])>1000){
                anws.push_back(vTransaction[i]);
                flag[i] = 1;
            }
            //和后面的交易做比较
            for(int j=i+1; j<size; j++){
                //已经被判定无效了,就不用管
                if(flag[j]==1)
                    continue;
                vector<string> anothertrans = words[j];//0姓名,1时间,2金额,3城市
                //名字相同,城市不同,时间差<=60
                if( (trans[0].compare(anothertrans[0])==0 && trans[3].compare(anothertrans[3])!=0 && ( abs( stoi(trans[1])-stoi(anothertrans[1]) ) <= 60 )) ){
                    //需要判断一下
                    if(flag[i]!=1){
                        anws.push_back(vTransaction[i]);
                        flag[i] = 1;
                    }
                    if(flag[j]!=1){
                        anws.push_back(vTransaction[j]);
                        flag[j] = 1;
                    }
                }
                
            }
        }
        return anws;
    }
private:
        vector<string> splitTrans(string input){
            vector<string> out;
            string word = "";
            for(int i=0; i<input.length(); i++){
                if(input[i]==','){
                    out.push_back(word);
                    word = "";
                }else{
                    word += input[i];
                }
            }
            out.push_back(word);
            return out;
        }
        
};




#笔试题目##Shopee#
全部评论
请问一下,我看之前上一次的笔试还有选择题,能不能说一下这一次的选择题都有些啥啊
点赞 回复
分享
发布于 2020-08-05 16:48
这是提前批还是正式批啊
点赞 回复
分享
发布于 2020-08-05 17:10
联想
校招火热招聘中
官网直投
shopee笔试完多久发面试通知?
点赞 回复
分享
发布于 2020-08-05 17:15
lz有笔试结果了吗
点赞 回复
分享
发布于 2020-08-10 10:56
欧气,楼主是深圳的吗?
点赞 回复
分享
发布于 2020-08-13 16:21

相关推荐

4 24 评论
分享
牛客网
牛客企业服务