看了 20 个 OA 帖, 4 道题有一道是原题, 还有一道是变种. Code signal 题目真的太多了, 所以不可能都看全. 最后 得了 844 分, 满分 855? 还是 1200? 不很清楚是怎么算的分数. 不过我每一道题都是 300/300 满分过的. 希望这个帖子能帮助到大家, 也希望大家给我加一点米
Quesiton 1
You are given an integer n. Your task is to calculate how many times the digits 0, 2 and 4 appear in all the non-negative integers up to n (0, 1, ...., n).
Example
• For n = 10, the output should be countOccurrences(n) = 4.
○ The digit 0 appears in numbers 0 and 10 once, for a total of 2 occurrences,
○ The digit 2 appears in the number 2 once, for a total of 1 occurrence,
○ The digit 4 appears in the number 4 once, for a total of 1 occurrence.So the answer is 2 + 1 + 1 = 4.
• For n = 22, the output should be countOccurrences(n) = 11.
○ The digit 0 appears in numbers 0, 10, and 20 once, for a total of 3 occurrences,
○ The digit 2 appears in numbers 2, 12, 20, and 21 once, and in the number 22 twice, for a total of 6 occurrences,
○ The digit 4 appears in numbers 4 and 14 once, for a total of 2 occurrences.So the answer is 3 + 6 + 2 = 11.
Input/Output
• [execution time limit] 4 seconds (py3)
• [input] integer nAn integer.Guaranteed constraints:0 ≤ n ≤ 1000.
• [output] integerThe overall count of occurrences of digits 0, 2 and 4 in the integers from 0 to n inclusive.
[Python 3] Syntax Tips
# Prints help message to the console# Returns a stringdef helloWorld(name): print("This prints to the console when you Run Tests") return "Hello, " + name
Quesiton 2
You are given a string str containing only the letters W, D, and L. Your task is to construct a new string from the characters of str, according to the following algorithm:
1. Begin with an empty string output = "".
2. If str contains a W, then remove it and concatenate a W to the end of output.
3. If str contains a D, then remove it and concatenate a D to the end of output.
4. If str contains an L, then remove it and concatenate an L to the end of output.
5. If str is empty, end the algorithm; otherwise go back to step 2.
Return the value of output after the algorithm is complete.
Note that it doesn't matter from where you remove the letter from the string, only the count of the letters left in the string matters.
Example
• For str = "LDWDL", the output should be equallyRearranging(str) = "WDLDL".
○ str contains all W, D, and L, so we add them to the output and remove from the initial string one by one and get "WDL"
○ The remaining string contains only letters D and L, so we a;b", "e", "a", "a"] and its corresponding cyclic string is "abeaa",
○ The diagonal with index 6 is ["c", "a", "c", "g"] and its corresponding cyclic string is "cacgc",
○ The diagonal with index 7 is ["a", "c", "b"] and its corresponding cyclic string is "acbac",
○ The diagonal with index 8 is ["b", "b"] and its corresponding cyclic string is "bbbbb",
○ The diagonal with index 9 is ["b"] and its corresponding cyclic string is "bbbbb".The lexicographical ordering of the matrix diagonals looks like ["aaaaa", "abeaa", "abeab", "acbac", "bbbbb", "bbbbb", "bbbbb", "cacgc", "cadbc"], so the answer is [1, 5, 3, 7, 2, 8, 9, 6, 4].Note that the cyclic string "bbbbb" occurs 3 times, and the indices corresponding to this cyclic string appear in ascending order in the output: [2, 8, 9].
Input/Output
• [execution time limit] 4 seconds (py3)
• [input] array.array.char aA square matrix of characters. It is guaranteed that the characters in the matrix are all lowercase English letters.Guaranteed constraints:2 ≤ a.length ≤ 100,a[i].length = a.length.
• [output] array.integerAn array of size 2n - 1, containing the indices of the diagonals after sorting.
[Python 3] Syntax Tips
# Prints help message to the console# Returns a stringdef helloWorld(name): print("This prints to the console when you Run Tests") return "Hello, " + name |