时间:2015-08-11 15:00 ~ 16:00
地点:北京邮电大学鸿通楼
类别:电面
1. 问简历。
2. 算法题:
平台:collabedit
/*
1. 将 string 转化为 integer
*/
#include <string> #include <climits> using std::string; string trim(const string &str) { int first_index = 0; while (first_index < str.size()) { if (str[first_index] != ‘ ‘) break; first_index++; } int last_index = str.size() - 1; while (last_index >= 0) { if (str[last_index] != ‘ ‘) break; last_index--; } return str.substr(first_index, last_index - first_index + 1); } // "abc" -> 1 -> not string // "123456789012345" -> 2 -> too long int last_err_code; // " 123456 " -> 123456 // " -123456" -> -123456 // "" -> 0 last_err_code = 1 // " abc" -> 0 last_err_code = 1 // "+" -> 0 last_err_code = 1 // "-" -> 0 last_err_code = 1 // "123456789012345" -> 0 last_err_code = 2 int string_to_int(const string &str) { int index = 0; bool is_negative = false; long long ret = 0; const string num_str = trim(str); if (num_str.size() == 0) { last_err_code = 1; return 0; } if (num_str[0] == ‘-‘) { is_negative = false; index = 1; } else if (num_str[0] == ‘+‘) { is_negative = true; index = 1; } if (index == 1 && index == num_str.size()) { // "+" // "-" last_err_code = 1; return 0; } while (index < num_str.size()) { if (num_str[index] < ‘0‘ || num_str[index] > ‘9‘) { // "abc" // "123cbd" last_err_code = 1; return 0; } ret = ret * 10 + (int)(num_str[index] - ‘0‘); if (ret > (long long)INT_MAX) { last_err_code = 2; return 0; } index++; } return is_negative ? -1 * (int)ret : (int)ret; }
3. 算法题:
平台:collabedit
/*
给两个递增的整数数组 A 和 B,长度分别为 n 和 m。找出这两个数组中第 K 小的数。
例如:A = [1,2, 4, 8, 10], B = [2, 3, 4, 5], K = 4。第 K 小的数是 3。
*/
int last_err_code; // Return -1 when error and set last_err_code int find_kth_small(int A[], int n, int B[], int m, int K) { int ans = 0; int index_a = 0; int index_b = 0; int index = 1; if (n < 0 || m < 0 || K > n + m || K <= 0) { last_err_code = 1; return -1; } while (index < K) { if (index_a < n && index_b < m) { if (A[index_a] < B[index_b]) index_a++; else index_b++; } else if (index_a < n) { index_a++; } else { index_b++; } index++; // ERROR 当时忘记对index进行自增了! } if (index_a < n && index_b < m) ans = A[index_a] < B[index_b] ? A[index_a] : B[index_b]; else if (index_a < n) ans = A[index_a]; else ans = B[index_b]; return ans; }
时间复杂度:O(K)
面试官让优化一下:
int last_err_code; // Return -1 when error and set last_err_code int find_kth_small(int A[], int n, int B[], int m, int K) { int a_left = 0; int b_left = 0; int k_2; int a_k_2; int b_k_2; if (n < 0 || m < 0 || K > n + m || K <= 0) { last_err_code = 1; return -1; } while (K > 2) { k_2 = (K - 1) / 2; a_k_2 = a_left + k_2; a_k_2 = a_k_2 < n - 1 ? a_k_2 : n - 1; b_k_2 = b_left + k_2; b_k_2 = b_k_2 < m - 1 ? b_k_2 : m - 1; if (A[a_k_2] == B[b_k_2]) { return A[a_k_2]; } else if (A[a_k_2] < B[b_k_2]) { K -= a_k_2 - a_left; a_left = a_k_2; if (a_left == n - 1) return B[b_left + K - 2]; } else { K -= b_k_2 - b_left; b_left = b_k_2; if (b_left == m - 1) return A[a_left + K - 2]; } } if (K == 1) return A[a_left] < B[b_left] ? A[a_left] : B[b_left]; return A[a_left] > B[b_left] ? A[a_left] : B[b_left]; }
时间: 2024-10-17 20:45:37