Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
You may return any answer array that satisfies this condition.
Example 1:
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
sudoku
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
Example 1:
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.
Success
Details
Runtime: 32 ms, faster than 12.85% of C++ online submissions for Valid Sudoku.
Memory Usage: 13.9 MB, less than 25.89% of C++ online submissions for Valid Sudoku.
for (int i=0; i<9; ++i) { columns.push_back(set<char>()); rows.push_back(set<char>()); blocks.push_back(set<char>()); }
int ln = 0; for (auto line : board) { int col = 0; for (auto c : line) { if (c=='.') { ++col; continue;} if (rows[ln].find(c) != rows[ln].end()) returnfalse; if (columns[col].find(c) != columns[col].end()) returnfalse; int blk = ln/3*3 + col/3; if (blocks[blk].find(c) != blocks[blk].end()) returnfalse; rows[ln].insert(c); columns[col].insert(c); blocks[blk].insert(c); ++col; } ++ln; } returntrue; } };
The Checklist
A function must pass two tests to be considered “pure”:
* Same inputs always return same outputs
* No side-effects
作者使用了几个简单的代码例子来举例说明了 Same Input => Same Output 感觉这几个例子有点刻意, 可以丑化了非纯函数的实现, 用来说明纯函数的优点的话感觉这里并不贴切, 毕竟,正常人也不能写出demo中的代码。
No Side-Effects
avoid side-effects
This test itself is a checklist. A few examples of side-effects are
Mutating your input
console.log
HTTP calls (AJAX/fetch)
Changing the filesystem (fs)
Querying the DOM
A Function’s pure if it’s free from side-effects and returns the same output, given the same input.
Side-effects include: mutating input, HTTP calls, writing to disk, printing to the screen.
You can safely clone, then mutate, your input. Just leave the original one untouched.
Spread syntax (… syntax) is the easiest way to shallowly clone objects.
JSON.parse(JSON.stringify(object)) is the easiest way to deeply clone objects. Thanks again Rodrigo Fernández Díaz!
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
大意
给出一个整数数组nums, 找出数组中所有的不同组合 a, b, c , 使之满足 a + b + c 之和等于0, 输出他们。
classSolution { public: vector<vector<int>> threeSum(vector<int>& nums) { int dimension = nums.size(); sort(nums.begin(), nums.end()); vector<vector<int>> result; for (int i=0; i<dimension-2; ++i) { if (nums[i] > 0) break; int f=i+1, t=dimension-1; while (f < t) { int sum = nums[i] + nums[f] + nums[t]; if (sum == 0) { result.push_back(vector<int>({nums[i], nums[f], nums[t]})); while (i < dimension-2 && nums[i]==nums[i+1]) {i++;} do {t--;} while(f < t && nums[t]==nums[t+1]); do {f++;} while (f < t && nums[f]==nums[f-1]); } elseif (sum > 0) { t--; } else { f++; } } } return result; } };
Result
Success
Details
Runtime: 100 ms, faster than 91.61% of C++ online submissions for 3Sum.
Memory Usage: 14.9 MB, less than 97.80% of C++ online submissions for 3Sum.
我是一个资深vimer, 我承认我现在分享这个内容真实丢vimer的脸。但是作为一个手动挡, 我一直都是这么做的。 每当要在文档中搜索或者替换一个内容的时候, 我都会不厌其烦的一个个字符敲入, :% s/search_my_content_balabalabala/i am the new balabalabala/g 然后认真反复检查3遍, 按下enter键, 一次替换完成, 我在过去的七八年里都是这样做的。