Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Checkerboard Pattern

Draw a checkerboard of # and . cells with the given number of rows and columns.

Bạn sẽ học được gì

  • Use r + c to decide between two alternating values
  • Build a rectangular grid row by row
  • Handle a grid with zero rows

Đề bài

Xem bản dịch

A checkerboard alternates two colors in both directions. Here a dark cell is # and a light cell is .. Number rows and columns from 0; the cell in row r and column c is # when r + c is even and . when it is odd, so the top-left cell is always #.

Implement checkerboard_pattern(rows, cols).

Input

rows: The number of rows.

cols: The number of columns.

Output

Return rows strings from top to bottom, each exactly cols characters long with no spaces. When rows is 0, return an empty list.

Ràng buộc

0 ≤ rows ≤ 60, 1 ≤ cols ≤ 60

Ví dụ

Ví dụ 1
Đầu vào:
rows = 3
cols = 4

Kết quả mong đợi:
expected = ["#.#.", ".#.#", "#.#."]
Đầu vào:
long long rows = 3;
long long cols = 4;

Kết quả mong đợi:
std::vector<std::string> expected = {"#.#.", ".#.#", "#.#."};
Đầu vào:
long rows = 3;
long cols = 4;

Kết quả mong đợi:
string[] expected = new string[] {"#.#.", ".#.#", "#.#."};

Giải thích

Row 0 starts with #, row 1 with ., row 2 with # again: ["#.#.", ".#.#", "#.#."].

Ví dụ 2
Đầu vào:
rows = 2
cols = 1

Kết quả mong đợi:
expected = ["#", "."]
Đầu vào:
long long rows = 2;
long long cols = 1;

Kết quả mong đợi:
std::vector<std::string> expected = {"#", "."};
Đầu vào:
long rows = 2;
long cols = 1;

Kết quả mong đợi:
string[] expected = new string[] {"#", "."};

Giải thích

One column: cell (0, 0) has an even sum and cell (1, 0) an odd one: ["#", "."].

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Neighbouring cells differ by one in either r or c, so r + c changes parity.

Gợi ý 2

Decide each cell from (r + c) mod 2.

Gợi ý 3

Every row has exactly cols characters.

Độ phức tạp mong đợi

O(rows * cols) time, O(rows * cols) extra space

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.