n log n−n log log n + O(n)
var
.var tempScrollTop
, currentScrollTop = 0
;
tempScrollTop
остаётся неинициализированной, её значение undefined
, а во 2-м варианте ей даётся начальное значение 0
. function sum2d(arr) {
var row, col, sum = arr[0].slice();
for( row = 1; row < arr.length; row++) {
for( col = 0; col < sum.length; col++) {
sum[col] += arr[row][col];
}
}
return sum;
}
sum2d([
[1,2,3],
[8,9,0],
])
// [9,11,3]
$param = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
$filters = explode('-', $param);
$banner = $country = NULL;
$tags = [];
if( count($filters)) {
foreach($filters AS $filter) {
if( preg_match('/^[0-9\.]+$/', $filter)) { // 1.5.9
$banner = $filter;
} elseif( preg_match('/^(RU|EN)$/', $filter) { // RU
$country = $filter;
} else { // tag
array_push($tags, $filter);
}
}
}
$conditions = [];
$params = [];
if($banner) {
array_push($conditions, "`banner` = ?");
array_push($params, $banner);
}
if($country) {
array_push($conditions, "`country` = ?");
array_push($params, $country);
}
if(count($tags)) {
foreach($tags AS $tag) array_push($params, $tag);
array_push($conditions, sprintf(
"`category_id` IN (SELECT `category_id` FROM `categories` WHERE `name` IN (%s)",
implode(',', str_split( str_repeat('?', count($tags))))
));
}
$query = 'SELECT * FROM sometable';
if( count($conditions)) $query = $query . ' WHERE ' . implode(' AND ', $conditions);
$stmt = $dbh->prepare($query);
if( $stmt->execute($params)) {
while ($row = $stmt->fetch()) {
print_r($row);
}
}
function* rnd(n) {
var nums = [], i, randomIndex, itemAtIndex;
for( i=0; i<n; nums.push(i++));
for( i=n-1; i>=0; i--) {
randomIndex = Math.floor(Math.random()*(i+1));
itemAtIndex = nums[randomIndex];
nums[randomIndex] = nums[i];
nums[i] = itemAtIndex;
}
i = n - 1
while(i >= 0)
yield nums[i--];
}
var gen = rnd(10);
while(true) {
var v = gen.next();
if( v.done) break;
console.log(v.value);
}
console.log('Done!');