面试题
文章平均质量分 53
每日一题
这个作者很懒,什么都没留下…
展开
-
Leetcode的Pow(x, n) 与 sqrt(x)
Pow(x,n):class Solution {public: double pow(double x, int n) { if(x == 0){ cout<<"NaN"<<endl; return -1; } if(n == 0){ return 1;原创 2014-12-24 04:21:15 · 327 阅读 · 0 评论 -
亚马逊前端开发人员面试题
第一面:1、问你知不知道javascript的closure的概念,问你如何利用其实现一个私有的不能被外界访问的变量;2、问你关于doctype的概念,以及浏览器如何实现这些doctype3、http的verb种类,以及get和post的区别和工作流程;4、在你之前的项目中如何实现前端代码的优化;5、编写一个简单的javascript程序,需要你使用array.prototyp原创 2014-12-03 12:14:24 · 3261 阅读 · 0 评论 -
LeetCode Two Sum III - Data structure design
class TwoSum {private: unordered_map buffer;public: void add(int number) { buffer[number]++; } bool find(int value) { for(auto elem : buffer){ int num1 = elem.second;原创 2014-12-26 14:05:25 · 987 阅读 · 0 评论 -
Leetcode Largest Number
class Solution {private: bool cmp(int a,int b){ stringstream ssa, ssb; string sa,sb; ssa << a << b; ssa >> sa; ssb << b << a;原创 2015-01-14 08:11:04 · 332 阅读 · 0 评论 -
概率和随机数经典面试问题:拒绝采样,蓄水池抽样,洗牌问题和随机01问题
已知rand7()[1..7],求产生rand10()[1..10]基本思路大家都知道的,就是call rand7()两次然后进行拒绝采样,因此大部分人第一下想到的算法如下:int rand10() { int row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row-1)*7;原创 2014-12-22 03:42:50 · 794 阅读 · 0 评论 -
Leetcode Climbing Stairs logN 做法
class _2by2{private: int d00,d01,d10,d11;public: _2by2(int d00,int d01,int d10,int d11):d00(d00),d01(d01),d10(d10),d11(d11){} void operator *= (const _2by2& rhs){ int td00原创 2014-12-23 14:24:54 · 554 阅读 · 0 评论 -
Leetcode Permutations II 最简单的解法
class Solution {private: bool nextPermutation(vector &num){ int index, size = num.size(), swapIndex; for(index = size - 2; index >= 0 && num[index] >= num[index + 1]; index--);原创 2014-12-23 14:33:32 · 324 阅读 · 0 评论 -
Leetcode Merge k Sorted Lists 利用stl heap来实现
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */struct Greater { bool operator() (ListN原创 2014-12-23 09:04:52 · 378 阅读 · 0 评论 -
Leetcode Wildcard Matching
class Solution {public: bool isMatch(const char *s, const char *p) { char *ss = NULL, *sp = NULL; while(*s){ if(*p=='?'||*s==*p){s++;p++;continue;} if(*p!=原创 2014-12-21 13:10:21 · 519 阅读 · 0 评论 -
Leetcode (没有转换long long类型) Fraction to Recurring Decimal
class Solution {public: string fractionToDecimal(int paraNumerator, int paraDenominator) { stringstream integralStream,decimalStream; string integral,decimal; if((paraNume原创 2014-12-17 05:11:57 · 554 阅读 · 0 评论 -
leetcode Reverse Words in a String II
class Solution {private: void reverseString(string &s,int i,int j){ while(i<j){ char tmp = s[i]; s[i] = s[j]; s[j] = tmp; i++;原创 2015-02-05 14:36:57 · 538 阅读 · 0 评论 -
Leet code Maximum Gap
Maximum Gap Question Solution Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Retur原创 2014-12-14 12:21:18 · 562 阅读 · 0 评论 -
Leetcode Binary Search Tree Iterator
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class BSTIte原创 2015-01-02 09:04:55 · 353 阅读 · 0 评论 -
判断整数乘法是否overflow
bool isOverflow(int a,int b){ //assume a and b is positive number if((a|b) > SQRT_INT_MAX && b && a > INT_MAX/b){ return true; }else{ return false; }}原创 2015-01-02 09:16:13 · 785 阅读 · 0 评论 -
Leetcode Median of Two Sorted Arrays
这是一道比较难的题目,因为正常考虑的话,想在面试中解出这道题,你必须知道在两个有序数组中找第K大数的程序,然后利用那程序来解出这道题,详细如下:这里要注意的是两个有序数组中找第K大数程序中K是从1开始的,不是从0开始的。class Solution {private: int findKth(int A[], int m, int B[], int n, int k){原创 2014-12-21 15:23:12 · 331 阅读 · 0 评论 -
Leetcode Factorial Trailing Zeroes
class Solution {public: int trailingZeroes(int n) { if(n<=4){ return 0; } int currAns, ans = 0; do{ ans += currAns = n / 5; n /原创 2015-01-01 15:27:37 · 451 阅读 · 0 评论 -
Leetcode Intersection of Two Linked Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private: inline int原创 2014-12-16 07:19:50 · 267 阅读 · 0 评论 -
Leetcode Repeated DNA Sequences
class Solution {private: unordered_map hash; unordered_map revert; string retrieve(int ans){ string ret = ""; for(int i = 0; i < 10; i++){ ret += revert[(ans >原创 2015-02-06 15:25:01 · 824 阅读 · 0 评论 -
Tree Walk 树三种遍历的OJ
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_C#include#include#include#includeusing namespace std;typedef struct node{ int id; node *left, *right; node(int i原创 2014-12-15 09:37:57 · 754 阅读 · 0 评论 -
Leet Code Binary Tree Inorder Traversal(非递归且实现不用栈的空间复杂度为O(1)的实现)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-12-14 14:31:13 · 411 阅读 · 0 评论 -
Leet Code Binary Tree Preorder Traversal(非递归且实现不用栈的空间复杂度为O(1)的实现)
class Solution {public: vector preorderTraversal(TreeNode *root) { vector ans; TreeNode *currNode; while(root){ while(root -> left && root -> right){原创 2014-12-15 07:18:02 · 396 阅读 · 0 评论 -
2015互联网校招总结—一路走来
转载请注明出处:http://blog.csdn.net/ns_code/article/details/40408397 写在前面 结束了在百度的实习,是时候写下校招的总结了,再不写估计很多东西都忘了。在开源社区混迹久了,从别人的学习、求职、工作经历中越发感受到很多的正能量,也本着攒RP的原则,向学弟学妹们,尤其非名校的学弟学妹们传递点正能量,因为博主也是非名校出身转载 2015-01-10 10:53:43 · 788 阅读 · 0 评论 -
Leetcode Regular Expression Matching 动态规划解法
class Solution {public: bool isMatch(const char *s, const char *p) { int i, j; int m = strlen(s); int n = strlen(p); /** * b[i + 1][j + 1]: if s[0..i] ma翻译 2014-12-21 06:00:01 · 3670 阅读 · 4 评论 -
Leetcode Missing Ranges
class Solution {private: vector ans; void addToAns(int a,int b){ if(b>=a){ stringstream stream; string res; stream<<a; if(b>a){原创 2014-12-16 05:55:28 · 699 阅读 · 0 评论 -
leetcode LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if原创 2014-11-24 00:29:06 · 297 阅读 · 0 评论 -
leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].原题:点击打开链接原创 2014-11-24 00:27:06 · 375 阅读 · 0 评论 -
leetcode Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", r原创 2014-11-21 00:57:07 · 313 阅读 · 0 评论 -
leetcode Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1原创 2014-11-21 00:32:51 · 311 阅读 · 0 评论 -
strstr模板
class Solution { public: int strStr(char *haystack, char *needle) { int Hlen = strlen(haystack), Nlen = strlen(needle), k=-1, j=0, i=0, *prev = new int[Nlen]; prev[0]=-1;转载 2014-11-20 01:45:34 · 329 阅读 · 0 评论 -
Leetcode Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2014-11-20 23:48:44 · 288 阅读 · 0 评论 -
什么样的遍历序列组合可以唯一地创建一棵二叉树
中序和前序中序和前序翻译 2014-11-20 01:12:37 · 846 阅读 · 0 评论 -
IT面试题---如何确定一棵二叉树是另一棵二叉树的子树(1)
给予两棵二叉树,如何确定第一棵二叉树是第二棵二叉树的子翻译 2014-11-20 00:50:50 · 397 阅读 · 0 评论 -
Leetcode Longest Substring with At Most Two Distinct Characters
class Solution {public: int lengthOfLongestSubstringTwoDistinct(string s) { int len = s.length(); unordered_map existingChars; int cnt = 0, longest = 0, st = 0; fo原创 2014-12-16 07:53:55 · 445 阅读 · 0 评论 -
LeetCode Binary Tree Upside Down
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-12-16 09:51:27 · 763 阅读 · 0 评论 -
面试题---名人识别问题
Celebrity problem: You have a room with n people. A celebrity walks in. Everyone knows the celebrity, the celebrity knows no one. Non-celebrities may/may not know anyone in the room. Give an algor原创 2014-12-18 13:47:50 · 716 阅读 · 0 评论 -
Leetcode Read N Characters Given Read4 (I,II同一款程序)
// Forward declaration of the read4 API.int read4(char *buf);const int size = 4;class Solution {private: char buffer[size+1]; int cnt; int readFromBuffer(char *buf, int n){原创 2014-12-16 14:27:55 · 585 阅读 · 0 评论 -
解决旋转数组二分查找的万能程序
class Solution {public: bool search(int A[], int n, int target) { int low = 0, high = n - 1; while(low <= high){ int mid = low + ((high - low) >> 1); if(A[原创 2014-12-02 20:51:02 · 392 阅读 · 0 评论 -
Leetcode Dungeon Game(经典动态规划)
class Solution {public: int calculateMinimumHP(vector > &dungeon) { int n = dungeon.size(); if(n <= 0){ return 0; } int m = dungeon[0].size();原创 2015-01-07 11:33:36 · 620 阅读 · 0 评论 -
Leetcode: Divide Two Integers
class Solution {public: int divide(int dividend, int divisor) { long long a = abs((double)dividend); long long b = abs((double)divisor); long long res = 0; i转载 2014-12-17 05:51:09 · 287 阅读 · 0 评论 -
Leetcode Find Peak Element
class Solution {public: int findPeakElement(const vector &num) { int size = num.size(); if(size==1){ return 0; }else{ int i=0,j=size-1;原创 2014-12-16 14:56:36 · 378 阅读 · 0 评论