Description 中文 English Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like sequence [123, 456, 579]. Formally, a Fibonacci-like sequence is a list F of non-negative integers such that: 0 <= F[i] <= 2^31 - 1, (that is, each integer fits a 32-bit signed integer type); F.length >= 3; and F[i] + F[i+1] = F[i+2] for all 0 <= i < F.length - 2. Also, note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself. Return any Fibonacci-like sequence split from S, or return [] if it cannot be done. 1 <= S.length <= 200 S contains only digits. Have you met this question in a real interview? Yes Problem Correction Example Example 1: Input: "123456579" Output: [123,456,579] Example 2: Input: "11235813" Output: [1,1,2,3,5,8,13] Example 3: Input: "112358130" Output: [] Ex...
这题要注意的是用一个map< int, int >来存储id和machine之间的映射关系。注意不能用map< int, vector > 来存储一个machine和一串id,这样就没法找某个id对应哪个machine了。 另外还要 用一个set来存储已经用过的id。 另外再强调map和set的key都是默认从大到小排序的。记得lower_bound()这个函数非常非常有用!map和set都可以用! class Solution { public: int n, k; map<int, int> shards; //stores id<->machine mapping, multiple ids to one machine set<int> ids; //stores ids to make sure it is not reused /* * @param n: a positive integer * @param k: a positive integer * @return: a Solution object */ static Solution create(int n, int k) { Solution sol = Solution(); sol.n = n; sol.k = k; return sol; } /* * @param machine_id: An integer ...
Comments
Post a Comment