Zocodc/Zillow/Qualtrics面试题

Zocodc:

难度:中等 / 困难

给一个长为 n 的整型数组 nums,返回一个数组 output 满足 output[i] = nums 中除 nums[i] 以外的元素的乘积。

要求在线性时间内解决,并且不使用除法。

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int size = nums.size();
        if(!size) {
            return nums;
        }    
        vector<int> cum(size, 1);
        for(int idx = 1; idx < size; idx++) {
            cum[idx] = cum[idx-1]*nums[idx-1];
        }
        int tmp = 1;
        for(int idx = size - 2; idx>= 0; idx--) {
            tmp *= nums[idx+1];
            cum[idx] *= tmp;
        }   

        return cum;
    }
};

Zillow:

难度:简单

在线性时间内找出一个字符串数组中第一个不重复的元素。

char FirstNonDuplicateChar(string str)
{
    int size = str.size();
    if(!size) {
        return NULL;
    }

    unordered_map<char, int> mp;

    for(int idx = 0; idx < size; idx++) {
        mp[str[idx]]++;
    }

    for(unordered_map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {
        if(it->second == 1) {
            return it->first; 
        }
    }

    return NULL;
}

Qualtrics

难度:中等给定一个二维数组以及起点和终点,如果能够找到一条从起点到终点的路径并且路径上的值是递减的,则返回 True。否则返回 False。| 100 | 40 | 20 || 80 | 30 | 10 || 100 | 101 | 0 |在这个例子中,起点是 (0,0),终点是 (2,2),能够找到这样一条满足条件的路径:100 -> 40 -> 20 -> 10 -> 0,所以返回 True。你可以往上下左右四个方向移动。

bool isPath(vector<vector<int>>& palace) 
{
    int row = palace.size();
    if(!row) {
        return true;
    }   
    int col = palace[0].size();

    return findPath(palace, row-1, col-1, 0, 0);
}

bool findPath(vector<vector<int>>&palace, int tar_row, int tar_col, int cur_row, int cur_col)
{
    if(tar_row == cur_row && tar_col == cur_col) {
        return true; 
    }

    if(cur_col > 0 && palace[cur_row][cur_col] < palace[cur_row][cur_col - 1]) {
        bool left = findPath(palace, tar_row, tar_col, cur_row, cur_col - 1);
        if(left) {
            return true;
        }
    }
    if(cur_col < tar_col && palace[cur_row][cur_col] < palace[cur_row][cur_col + 1]) {
        bool right = findPath(palace, tar_row, tar_col, cur_row, cur_col + 1);
        if(right) {
            return true;
        }
    }
    if(cur_row > 0 && palace[cur_row][cur_col] < palace[cur_row - 1][cur_col]) {
        bool tp = findPath(palace, tar_row, tar_col, cur_row - 1, cur_col);
        if(tp) {
            return true;
        }
    }
    if(cur_row < tar_row && palace[cur_row][cur_col] < palace[cur_row + 1][cur_col]) {
        bool dw = findPath(palace, tar_row, tar_col, cur_row + 1, cur_col);
        if(dw) {
            return true;
        }
    }

    return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值