简单-5

1260. 二维网格迁移


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;
    }
}

1266. 访问所有点的最小时间


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;
    }
}

1287. 有序数组中出现次数超过25%的元素

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;
    }
}

1290. 二进制链表转整数

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;
    }
}

1295. 统计位数为偶数的数字

image.png
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;
    }
}

1299. 将每个元素替换为右侧最大元素

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;
    }
}

1304. 和为零的N个唯一整数

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;
    }
}

1313. 解压缩编码列表

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;
    }
}

1331. 数组序号转换

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;
    }
}

1337. 方阵中战斗力最弱的 K 行

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;
    }
}


1346. 检查整数及其两倍数是否存在

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;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。