| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FlippingAnImage; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FlippingAnImageTwoPointers : IFlippingAnImage |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n * m) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="image"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int[][] FlipAndInvertImage(int[][] image) |
| 2 | 24 | | { |
| 20 | 25 | | foreach (var row in image) |
| 7 | 26 | | { |
| 7 | 27 | | var left = 0; |
| 7 | 28 | | var right = row.Length - 1; |
| | 29 | |
|
| 18 | 30 | | while (left < right) |
| 11 | 31 | | { |
| 11 | 32 | | (row[left], row[right]) = (row[right] ^ 1, row[left] ^ 1); |
| | 33 | |
|
| 11 | 34 | | left++; |
| 11 | 35 | | right--; |
| 11 | 36 | | } |
| | 37 | |
|
| 7 | 38 | | if (left == right) |
| 3 | 39 | | { |
| 3 | 40 | | row[left] ^= 1; |
| 3 | 41 | | } |
| 7 | 42 | | } |
| | 43 | |
|
| 2 | 44 | | return image; |
| 2 | 45 | | } |
| | 46 | | } |