Skip to main content
  1. Categories/

面试

2017

leetcode 495. Teemo Attacking

·1 min
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition.

leetcode 55. Jump Game (dp)

·1 min
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]. 思路:dp[i]表示能否到达位置i…无脑dp即可。。。

leetcode 56. Merge Intervals (模拟,求相交区间)

·1 min
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]. 思路:扫一遍即可。。 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月11日 星期二 19时15分30秒 4File Name :56.cpp 5************************************************ */ 6/** 7 8 * Definition for an interval. 9 10 * struct Interval { 11 12 * int start; 13 14 * int end; 15 16 * Interval() : start(0), end(0) {} 17 18 * Interval(int s, int e) : start(s), end(e) {} 19 20 * }; 21 22 */ 23 24class Solution { 25 26public: 27 28 int n; 29 static bool cmp(Interval A,Interval B) 30 { 31 return A.start<B.start; 32 } 33 vector<Interval> merge(vector<Interval>& pi) { 34 vector<Interval>res; 35 n = pi.size(); 36 if (n==0) return res; 37 sort(pi.begin(),pi.end(),cmp); 38 int l = -1,r = -1; 39 for ( int i = 0 ; i < n ; i++) 40 { 41 if (l==-1&&r==-1) 42 { 43 l = pi[0].start; 44 r = pi[0].end; 45 continue; 46 } 47 if (pi[i].start<=r) 48 { 49 r = max(r,pi[i].end); 50 continue; 51 } 52 if (pi[i].start>r) 53 { 54 res.push_back(Interval(l,r)); 55 l = pi[i].start; 56 r = pi[i].end; 57 continue; 58 } 59 } 60 //最后一组不要忘记 61 res.push_back(Interval(l,r)); 62 int siz = res.size(); 63 for ( int i = 0 ; i < siz ;i++) printf("%d ",res[i].start,res[i].end); 64 65 66 return res; 67 68 } 69 70};

leetocde 63. Unique Paths II

·1 min
Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid.

leetcode 64. Minimum Path Sum (二维dp)

·1 min
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.

leetcode 73. Set Matrix Zeroes (矩阵置0,乱搞)

·2 mins
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. **Follow up:**Did you use extra space? A straight forward solution using O(m__n) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution?

leetcode 79. Word Search (dfs)

·1 min
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

leetcode 289. Game of Life (模拟)

·3 mins
According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.” Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构建二叉树)

·1 min
/* *********************************************** 1Author :111qqz 2Created Time :2017年04月05日 星期三 16时49分57秒 3File Name :106.cpp 4************************************************ */ 5/** 6 * Definition for a binary tree node. 7 * struct TreeNode { 8 * int val; 9 * TreeNode *left; 10 * TreeNode *right; 11 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 * }; 13 */ 14class Solution { 15public: 16 TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { 17 int siz = inorder.size(); 18 if (siz==0) return NULL; 19 int rt = postorder[siz-1]; 20 int pos = -1; 21 for ( int i = 0 ; i < siz; i++) 22 { 23 if (inorder[i]==rt) 24 { 25 pos = i ; 26 break; 27 } 28 } 29 TreeNode *head = new TreeNode(rt); 30 vector<int>in,post; 31 for ( int i = 0 ; i < pos ; i++) 32 { 33 in.push_back(inorder[i]); 34 post.push_back(postorder[i]); 35 } 36 head->left = buildTree(in,post); 37 in.clear(); 38 post.clear(); 39 for ( int i = pos + 1 ; i < siz ; i++) 40 { 41 in.push_back(inorder[i]); 42 post.push_back(postorder[i-1]); 43 } 44 head->right = buildTree(in,post); 45 return head; 46 } 47};