#include <iostream>
using namespace std;
#define M 5
#include<stdlib.h>
//原矩阵,某元素第n行第m列,;顺时针旋转90度后,位置变成倒数第n列,第m行
//即先转置再水平翻转
int n = 0;
void rotation_90(int matrix[][M],int n)
{
for (int i = 0; i < n; i++)
{
for (int j = i; j < M; j++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
//转置
//水平翻转
for (int i = 0; i < n; i++)
{
for (int j = 0; j < M / 2; j++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][M - j - 1];
matrix[i][M - j - 1] = tmp;
}
}
}
int test01()
{
int matrix[][M] = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25} };
int n = sizeof(matrix) / (M * 4);
//行数
for (int i = 0; i < M; i++)
{
for (int j = 0; j < M; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
rotation_90(matrix,n);
cout << endl;
// 输出矩阵
for (int i = 0; i < M; i++)
{
for (int j = 0; j < M; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
//力扣题
//34. 在排序数组中查找元素的第一个和最后一个位置
//给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。请你找出给定目标值在数组中的开始位置和结束位置。
//
//如果数组中不存在目标值 target,返回[-1, -1]。
//
//你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
int* res = (int*)malloc(sizeof(int) * 2);
int r = numsSize - 1;//右边界 right
int l = 0; //左边界left
int found = 1; //用来判断是否找到
res[0] = -1;
res[1] = -1;
*returnSize = 2;
while (l <= r && found)
{
//找不到的情况
if (target < nums[l] || target > nums[r]) {
return res;
}
int mid = l + (r - l) / 2;
if (nums[mid] > target)
r = mid - 1;
else if (nums[mid] < target)
{
l = mid + 1;
}
else {
//左边界
found = 0;//找到后不再进行循环
res[0] = mid;
res[1] = mid;
int n = mid;
while (n > l)
{
--n;
if (nums[n] == target)
{
res[0] = n;
}
else
{
break;
}
}
//右边界
while (mid < r)
{
++mid;
if (nums[mid] == target)
{
res[1] = mid;
}
else
{
break;
}
}
}
}
return res;
}
void test02()
{
int arr[6] = { 5,7,7,8,8,10 };
int* nums = arr;
int* a = new int;
int* b = searchRange(nums, 6, 6, a);
cout << b[0] << b[1] << endl;
cout << *a;
}
int main()
{
test01();
test02();
return 0;
}