![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试题
logic_nut
这个作者很懒,什么都没留下…
展开
-
Leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/参考了http://blog.csdn.net/whuwangyi/article/details/14103993/** * Definition for singly-linked list. * struct ListNode { * int val; *原创 2014-01-29 00:39:51 · 844 阅读 · 0 评论 -
Leetcode: Remove Nth Node From End of List
http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :原创 2013-10-28 08:13:17 · 590 阅读 · 0 评论 -
Leetcode: Remove Duplicates from Sorted Array II
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/class Solution {public: int removeDuplicates(int A[], int n) { int current=0; for(int i=0;i<n;i++){原创 2013-10-28 11:31:29 · 513 阅读 · 0 评论 -
Leetcode: Remove Duplicates from Sorted Array
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/class Solution {public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below原创 2013-10-16 05:37:36 · 439 阅读 · 0 评论 -
Leetcode: Single Number II
http://oj.leetcode.com/problems/single-number-ii/#include "stdio.h"#include #include using namespace std;// Took more than two hours// Bit operation is not trival// Record the number of原创 2013-10-28 10:55:33 · 966 阅读 · 0 评论 -
Leetcode: Balanced Binary Tree
http://oj.leetcode.com/problems/balanced-binary-tree//** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x原创 2013-10-17 06:58:30 · 673 阅读 · 0 评论 -
Leetcode: Search a 2D Matrix
http://oj.leetcode.com/problems/search-a-2d-matrix/Take the matrix as a sorted list, and then use binary search directly.class Solution {public: int GetValue(vector > &matrix, int index原创 2013-10-17 06:58:13 · 603 阅读 · 0 评论 -
Leetcode: Container With Most Water
http://oj.leetcode.com/problems/container-with-most-water/// It's not easy to get the O(N) solution directly.// The idea is to use this conclusion: if(height[i]=MaxArea(i+1,j)>=MaxArea(i,j-1)原创 2013-10-17 06:58:07 · 533 阅读 · 0 评论 -
Leetcode: Minimum Path Sum
http://oj.leetcode.com/problems/minimum-path-sum/// The problem is straightforward, and the question is how to write it in an elegant wayclass Solution {public: const int INF=100000000;原创 2013-10-17 06:57:57 · 647 阅读 · 0 评论 -
Leetcode: Generate Parentheses
http://oj.leetcode.com/problems/generate-parentheses/// Interesting questions// It is very easy to generate duplicated result with normal dp// The core idea is that from the start to the end原创 2013-10-17 06:57:21 · 634 阅读 · 0 评论 -
Leetcode: Integer to Roman
http://oj.leetcode.com/problems/integer-to-roman/class Solution {public: string intToRoman(int num, int pos){ char *cc="IVXLCDM "; string res; char a=cc[pos*2],b=原创 2013-10-17 06:58:23 · 633 阅读 · 0 评论 -
Leetcode: Set Matrix Zeroes
http://oj.leetcode.com/problems/set-matrix-zeroes/// For constant memory requirement, we normaly have two ways:// 1. Finding a way to use a few variables to represent the status// 2. Using t原创 2013-10-28 13:21:18 · 752 阅读 · 0 评论 -
Leetcode: Search in Rotated Sorted Array II
http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/class Solution {public: // The time complexity is O(N) in worest case // For example, A[]={1,1,1,1,1,1,1,1,1,1} and t原创 2013-10-29 08:33:00 · 659 阅读 · 0 评论 -
Leetcode: Search in Rotated Sorted Array
http://oj.leetcode.com/problems/search-in-rotated-sorted-array/class Solution {public: // The key idea is that the start value and end value // can limite possible values of the array原创 2013-10-29 08:30:38 · 578 阅读 · 0 评论 -
Leetcode: Permutations and Permutations II
http://oj.leetcode.com/problems/permutations/class Solution {public: void Permute(vector &num, vector &used, int depth, vector> &res, vector ¤t){ if(depth==num.size()){原创 2013-10-30 08:35:27 · 1217 阅读 · 1 评论 -
Leetcode: Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/class Solution {public: // !!! // root==NULL doesn't indicate leaf node // root->left==root->right==NULL does indicate原创 2013-10-29 08:45:34 · 595 阅读 · 0 评论 -
Leetcode: Sum Root to Leaf Numbers
http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/// Tried many times to get accepted// Take care:// 1. root is NULL// 2. node with one child// 3. leafclass Solution {public:原创 2013-10-29 08:18:50 · 728 阅读 · 0 评论 -
Leetcode: Sort Colors
http://oj.leetcode.com/problems/sort-colors/// The easy way is to use count sort, but it is going to need two-pass// The solution below need one-pass.// The idea is to keep all the numbers b原创 2013-10-28 12:56:36 · 640 阅读 · 0 评论 -
Leetcode: Jump Game
http://oj.leetcode.com/problems/jump-game/class Solution {public: // Don't code too fast // The bad dp algorithm below (commented out) has the complexity of O(N*N) /*bool search(i原创 2013-10-29 11:16:49 · 763 阅读 · 0 评论 -
Trapping Rain Water
http://oj.leetcode.com/problems/trapping-rain-water/// Find the solution from the internet// For each individual slot, the capacity depends on the leftHighest and rightHighest// The naive s原创 2013-10-29 10:56:29 · 827 阅读 · 0 评论 -
Leetcode: Search for a Range
http://oj.leetcode.com/problems/search-for-a-range/class Solution {public: // Don't try to find one and then extend to the start direction and end direction // The time complexity wil原创 2013-10-29 08:56:22 · 750 阅读 · 0 评论 -
Leetcode: Combinations
http://oj.leetcode.com/problems/combinations/class Solution {private: vector used; vector > res; vector current;public: // It asks for all the combinations, so we need the va原创 2013-10-29 08:34:30 · 648 阅读 · 0 评论 -
Leetcode: 3Sum Closest
http://oj.leetcode.com/problems/3sum-closest/class Solution {public: // Similar to 3Sum Equal // Enumerate the minimal number, and then it becomes similar to 2Sum Equal // The ove原创 2013-10-29 08:29:35 · 662 阅读 · 0 评论 -
Leetcode: Subsets II
http://oj.leetcode.com/problems/subsets-ii/class Solution {public: // First, we sort the input set // Second, whenever we have a new element, we count the number of this element /原创 2013-10-29 09:19:27 · 709 阅读 · 0 评论 -
Leetcode: Best Time to Buy and Sell Stock
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/class Solution {public: int maxProfit(vector &prices) { // Note: The Solution object is instantiated only once and原创 2013-10-17 06:58:19 · 831 阅读 · 0 评论 -
Leetcode: Rotate Image
http://oj.leetcode.com/problems/rotate-image/// Do it in place is not easyclass Solution {public: void rotate(vector > &matrix) { int size=matrix.size(); for(int i=0;i<(s原创 2013-10-17 06:58:01 · 717 阅读 · 0 评论 -
Leetcode: Swap Nodes in Pairs
http://oj.leetcode.com/problems/swap-nodes-in-pairs//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(原创 2013-10-16 05:58:18 · 709 阅读 · 0 评论 -
Leetcode: Search Insert Position
http://oj.leetcode.com/problems/search-insert-position/class Solution {public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below //原创 2013-10-16 05:36:41 · 508 阅读 · 0 评论 -
Leetcode: Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/class Solution {public: int dp[10000]; int DP(int n){ if(dp[n]<0) dp[n]=DP(n-1)+DP(n-2); return dp[n]; } int cl原创 2013-10-16 05:34:50 · 473 阅读 · 0 评论 -
Leetcode: Symmetric Tree
http://oj.leetcode.com/problems/symmetric-tree//** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va原创 2013-10-16 05:32:24 · 539 阅读 · 0 评论 -
Leetcode: Same Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2013-10-16 05:24:41 · 463 阅读 · 0 评论 -
Leetcode: Convert Sorted Array to Binary Search Tree
http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree//** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;原创 2013-10-16 05:40:40 · 498 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/Leetcode上的一道题目,本以为挺简单,但最终花了不少时间,并且写出来的代码还特别丑陋。主要是两点。一是注意这种需要用递归来生成结点的问题,生成结点时候都需要用new,不然都是局部变量。二是这道题目需要求中间节点的序号,我没有想到特别好的原创 2013-09-20 13:59:04 · 1361 阅读 · 0 评论 -
codility上的一道题
有一个N*N的网格,给定一个边的数组,依次从网格中去掉数组里的边,问去掉多少条边后网格中点(0,0)和点(N-1,N-1)不再连通。这个问题可以转化成变种最短路径问题。初始化每条边的权值为MAX_INT,然后把数组中的边的权值改为这条边在数组中的顺序。这样求从点(0,0)到点(N-1,N-1)的距离就可以。不过公式是newNode.weight = min(edge(from原创 2012-11-10 09:28:21 · 1313 阅读 · 2 评论 -
C++中map的基本用法
在C++里面,一般就用map当作hashtable来用了。下面的代码演示了map的基本语法。#include "stdio.h"#include "stdlib.h"#include #include using namespace std;int main(){ const int num=4; map contact; string name[num]={"jinfu原创 2012-11-04 11:53:38 · 904 阅读 · 0 评论 -
Reservoir sampling 数组随机等概率选择样品
给定一个数组,数组大小N,随机选择K个样品。如果K比较小,可以随机生成K个在[0,N)之间的不重复的数,然后以这K个数作为index在原始数组中选择样品。但是如果K比较大,比如N=100000000,K=10000000,那么如何快速生成K个限定范围的不重复的随机数?可以构造一个大小为N的循环链表,初始化一个指针指向起点。随机生成一个数r,把指针往后移动r个位置,选择这个数作为原创 2012-11-04 13:08:33 · 497 阅读 · 0 评论 -
寻找无序数组中的第K大数和前K大数
两个问题互相可以转化。如果可以找到第K大数,那么只再需要O(N)就可以找齐剩余的前K大数。如果可以找到前K大数,那么只再需要O(K)就可以找到第K大数。先排序,在找第K个。O(NlgN)快速排序的思想,可以做到平均效率O(N)随机选一个元素,把所有小于等于这个元素的数移到左边,所有大于这个元素的数移动到右边。如果这个元素成了第K个数,直接返回这个数。如果左边的个数大原创 2012-11-03 13:29:34 · 5053 阅读 · 3 评论 -
Leetcode: Maximum Subarray
http://oj.leetcode.com/problems/maximum-subarray/class Solution {public: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int ma原创 2013-10-16 05:51:46 · 632 阅读 · 0 评论 -
Leetcode: Roman to Integer
http://oj.leetcode.com/problems/roman-to-integer/class Solution {public: int romanToInt(string s) { // Start typing your C/C++ solution below // DO NOT write int main() fu原创 2013-10-16 05:56:24 · 524 阅读 · 0 评论 -
Leetcode: Single Number
http://oj.leetcode.com/problems/single-number/class Solution {public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by ea原创 2013-10-16 05:57:33 · 871 阅读 · 1 评论