摘要:
今天看到一段代码,顺时针打印数组。写得太美了,简洁明了,几乎不用任何注释。转过来,原来是java版的,我改成了C++版。 1 void PrintMatrixClockWise() 2 { 3 int m = 5; 4 int n = 4; 5 int val[4][5] = 6 { 7 {1, 2, 3, 4, 5 }, 8 {6, 7, 8, 9, 1 }, 9 {11, 12,... 阅读全文
摘要:
题义很明细:求一个字符串S中的最长回文! 基本想法: for-loop i从0 – (n-1)遍历该字符串,从S[i] 或者 S[i+1]开始,向字符串S的两侧展开,判断是不是回文。如果是,再和当前的最长回文比较,如果更长,则替换当前最长的回文。 代码如下: // 从c1, c2开始 向两侧展开,找到最长的palindrome // e.g. 如果已经知道 "aba"是字符串"cabac"的一个字串,palindrome, // 则向两侧展开时,"c" == "c", 即 s[l] == s[r],所以能够继续得到 阅读全文
摘要:
很老的题目了,但是看到一种方法,非递归,贪心算法,遍历所有的可能的组合,并打印所有的步骤。void Calc_floors(){ int n=1; for (int X=0; X <=20; X++) { for (int Y=0; Y <=10; Y++) { for (int Z=0; Z <7; Z++) { if (X + 2*Y + 3*Z == 20) ... 阅读全文
摘要:
You have been given three arrays A,B and C.You have to find out all the elements in A and B such that the A[i]-B[j]=C[k]A:1. Put all the elements of array C in a HashTable - O(n) time and O(n) space2. Calculate all the possible differences of A[i] and B[j]. and check whether diff is present in HT or 阅读全文
摘要:
Q:There are a row of houses, each house can be painted with three colors red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How 阅读全文
摘要:
Count smaller elements on right side in an array.eg : [4,12,5,6,1,34,3,2]o/p: [3,5,3,3,0,2,1,0]A1: 设原数组为a[i],output 的数组为b[i]1. 从右开始向左扫描2. 假设已经扫描到i,则遍历从a[i+1] 开始,到结束的所有数,找到j,使得 a[i] > a[j], 并且 a[j] 值最大,则b[i] = b[j] + 1; 如果没有找到这样的数,即后面所有的数a[i] < a[j],则b[i] = 0;该方向时间复杂度为O(n^2)下面的方法是O(nlog2n)的方法A2 阅读全文
摘要:
First, and only for the constructor of the most derived class as described below, virtual base classes shall be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base cl 阅读全文
摘要:
c++ 中的 mutable 关键字是什么意思?mutable is for the case where an object islogicallyconstant, but in practice needs to change.class Employee {public: Employee(const std::string & name) : _name(name), _access_count(0) { } void set_name(const std::string & name) { _name = name; } std... 阅读全文
摘要:
Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.Q: 题意很明确,实现一个函数,如果该函数在过去的t秒内,被call了x次,则返回trueA:1. 对一个queue包装一下,其size只有x2. 每次call 这个函数的时候,检查queue是否的size==x,如果满了,pop第一个,再添加。每次添加当时的时间到queue中3. 函数返 阅读全文
摘要:
转化一下,There are two sorted arrays A and B of sizemandnrespectively. Find the median of the two sorted arrays. The overall run time complexity should beO(log (m+n)).有两个数组,如何求两个数组的中间值?找到一篇文章,慢慢读之:http://www.leetcode.com/2011/03/median-of-two-sorted-arrays.html另:自己想了一个方法 (passed testing)使用辅助数组k1, k2来记录对 阅读全文