class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
// Repeat the transform k times.
for (;k > 0; k--) {
int previous = grid[grid.length - 1][grid[0].length - 1];
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
int temp = grid[row][col];
grid[row][col] = previous;
previous = temp;
}
}
}
// Copy the grid into a list for returning.
List<List<Integer>> result = new ArrayList<>();
for (int[] row : grid) {
List<Integer> listRow = new ArrayList<>();
result.add(listRow);
for (int v : row) listRow.add(v);
}
return result;
}
}
class Solution {
public int minTimeToVisitAllPoints(int[][] points) {
int ans = 0;
int x0 = points[0][0];
int y0 = points[0][1];
for(int i = 1; i < points.length; i++){
int x1 = points[i][0];
int y1 = points[i][1];
ans += Math.max(Math.abs(y1-y0),Math.abs(x1-x0));
x0 = x1;
y0 = y1;
}
return ans;
}
}
class Solution {
public int findSpecialInteger(int[] arr) {
int count = 0;
int cur = arr[0];
for(int i = 0;i<arr.length;i++){
if(cur==arr[i]){
count++;
if(count*4>arr.length){
return arr[i];
}
}else{
cur = arr[i];
count = 1;
}
}
return -1;
}
}
class Solution {
public int getDecimalValue(ListNode head) {
ListNode cur = head;
int num = 0;
while(cur!=null){
num = num*2+cur.val;
cur = cur.next;
}
return num;
}
}
class Solution {
public int findNumbers(int[] nums) {
int ans = 0 ;
for(int n : nums){
if(((int)Math.log10(n)+1)%2==0){
ans++;
}
}
return ans;
}
}
class Solution {
public int[] replaceElements(int[] arr) {
int[] temp = new int[arr.length];
temp[temp.length-1] = -1;
for(int i=arr.length-2;i>=0;i--){
temp[i] =Math.max(temp[i+1],arr[i+1]);
}
return temp;
}
}
class Solution {
public int[] sumZero(int n) {
int[] ans = new int[n];
int index=0;
for(int i=1;i<=n/2;i++){
ans[index++] = i;
ans[index++] = -i;
}
return ans;
}
}
class Solution {
public int[] decompressRLElist(int[] nums) {
int length = 0;
for(int i = 0;i<nums.length;i+=2){
length += nums[i];
}
int[] ans = new int[length];
int index = 0;
for(int i = 0;i<nums.length;i+=2){
for(int j = 0;j<nums[i];j++){
ans[index++] = nums[i+1];
}
}
return ans;
}
}
class Solution {
public int[] arrayRankTransform(int[] arr) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int a : arr){
max = Math.max(max,a);
min = Math.min(min,a);
}
int[] tem = new int[max-min+1];
// 标记出现
for(int a : arr){
tem[a-min] = 1;
}
int index = 1;
// 按顺序标记索引
for(int i=0;i<tem.length;i++){
if(tem[i]==1){
tem[i] = index++;
}
}
int[] ans = new int[arr.length];
for(int i=0;i<arr.length;i++){
ans[i] = tem[arr[i]-min];
}
return ans;
}
}
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
List<int[]> list = new ArrayList<>();
for(int i = 0;i<mat.length;i++){
int count = 0;
for(int m : mat[i]){
if(m==1){
count++;
}else{
break;
}
}
list.add(new int[]{count,i});
}
Collections.sort(list,(x,y)->x[0]-y[0]);
int[] ans = new int[k];
for(int i = 0;i<k;i++){
ans[i] = list.get(i)[1];
}
return ans;
}
}
class Solution {
public boolean checkIfExist(int[] arr) {
Map<Integer,Integer> map = new HashMap<>();
for(int a : arr){
if(map.containsKey(a*2)){
return true;
}else if(a%2==0&&map.containsKey(a/2)){
return true;
}else{
map.put(a,1);
}
}
return false;
}
}