int[][] spiral(int n);
input = 3
123
894
765
input = 4
01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07
input = 8
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
import java.util.Arrays;
public class Spiral {
public static int[][] genSpiral(int n) {
if (n<=0) {
throw new IllegalArgumentException("N must be >0");
}
int[] dc = new int[]{1,0,-1,0};
int[] dr = new int[]{0,1,0,-1};
int dir = 0, val=0, r=0, c=0,limit=n*n;
int[][] matrix = new int[n][n];
while (val++ < limit) {
matrix[r][c] = val;
r += dr[dir];
c += dc[dir];
if (isInvalid(matrix,r, c)) {
r-= dr[dir];
c-=dc[dir];
dir = (dir+1)%4;
r+= dr[dir];
c+= dc[dir];
}
}
return matrix;
}
private static boolean isInvalid(int[][] m, int r, int c) {
return r<0||c<0||r>=m.length||c>= m.length||m[r][c] != 0;
}
}
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
<?php
print_look_and_say_seq(100);
function print_look_and_say_seq($count = 0) {
$val = 1;
for ($i = 1; $i <= $count; $i++) {
echo "$val\n";
$val = output_val($val);
}
}
function output_val($input) {
$input = "$input"; // Normalize from int to string
if (strlen($input) == 1) {
return "1$input";
}
$ret = '';
$cur = $input[0];
$count = 1;
for ($i = 1; $i <= strlen($input); $i++) {
if ($cur != $input[$i] || $i == strlen($input)) {
$ret .= "$count$cur";
$count = 1;
$cur = $input[$i];
} else {
$count++;
}
}
return $ret;
}
bool OneEditApart(string s1, string s2);
OneEditApart("cat", "dog") = false
OneEditApart("cat", "cats") = true
OneEditApart("cat", "cut") = true
OneEditApart("cat", "cast") = true
OneEditApart("cat", "at") = true
OneEditApart("cat", "act") = false
bool one_edit_apart(const string* s1, const string* s2) {
if (s1->size() > s2->size())
swap(s1, s2);
if (s2->size() - s1->size() > 1)
return false;
bool saw_difference = false;
for (size_t i = 0, j = 0; i < s1->size(); ++i, ++j) {
if ((*s1)[i] != (*s2)[j]) {
if (saw_difference) return false;
saw_difference = true;
if (s2->size() > s1->size()) {
--i;
}
}
}
return saw_difference || s2->size() != s1->size();
}
Equal Employment Opportunity
Meta is proud to be an Equal Employment Opportunity employer. We do not discriminate based upon race, religion, color, national origin, sex (including pregnancy, childbirth, reproductive health decisions, or related medical conditions), sexual orientation, gender identity, gender expression, age, status as a protected veteran, status as an individual with a disability, genetic information, political views or activity, or other applicable legally protected characteristics. You may view our Equal Employment Opportunity notice here.
Meta is committed to providing reasonable support (called accommodations) in our recruiting processes for candidates with disabilities, long term conditions, mental health conditions or sincerely held religious beliefs, or who are neurodivergent or require pregnancy-related support. If you need assistance or an accommodation due to a disability, fill out the Accommodations request form .