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;
}