7.26题解

59.螺旋矩阵

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int[][] dir = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
        int ptr = 0;
        int x = 0;
        int y = 0;
        for(int i=1;i<=n*n;i++){
            res[x][y] = i;
            if(ptr==0&&(y==n-1||res[x][y+1]!=0))
                ptr++;
            else if(ptr==1&&(x==n-1||res[x+1][y]!=0))
                ptr++;
            else if(ptr==2&&(y==0||res[x][y-1]!=0))
                ptr++;
            else if(ptr==3&&(res[x-1][y]!=0))
                ptr = 0;
            x += dir[ptr][0];
            y += dir[ptr][1];  
        }
        return res;
    }
}
class Solution {
    public int[][] generateMatrix(int n) {
        int i = 0;
        int[][] matrix = new int[n][n];
        int left = 0;
        int right = n-1;
        int top = 0;
        int bottom = n-1;
        while(i<n*n){
            if(left<=right){
                for(int j=left; j<=right; j++){
                    i++;
                    matrix[top][j] = i; 
                }
                top++;
            }else{
                break;
            }

            if(top<=bottom){
                for(int j=top; j<=bottom; j++){
                    i++;
                    matrix[j][right] = i; 
                }
                right--;
            }else{
                break;
            }

            if(left<=right){
                for(int j=right; j>=left; j--){
                    i++;
                    matrix[bottom][j] = i; 
                }
                bottom--;
            }else{
                break;
            }

            if(top<=bottom){
                for(int j=bottom; j>=top; j--){
                    i++;
                    matrix[j][left] = i; 
                }
                left++;
            }else{
                break;
            }
        }
        return matrix;
    }
}

77.组合

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    int u;
    int n;
    int[] used ;
    public List<List<Integer>> combine(int c, int k) {
        u = k;
        n = c;

        used = new int[n+1];
        dfs(0,1);
        return res;

    }
    // x 代表 已经进入了几个数了 now 代表当前数字的起点是几 这里面是不能回头找 比如【3,1】这种的
    public void dfs(int x,int now){
        if(x== u){
            res.add(new ArrayList<>(temp));
        }
        else{
            for(int i=now;i<=n;i++){
                if(used[i]==0){
                    used[i] = 1;
                    temp.add(i);
                    dfs(x+1,i);
                    used[i] = 0;
                    temp.remove(temp.size()-1);
            }
        }
    }
}
}
class Solution {
    private List<List<Integer>> res;
    public List<List<Integer>> combine(int n, int k) {
        res = new ArrayList<>();
        LinkedList<Integer> track = new LinkedList<>();
        backtrack(1, n, k , track);
        return res;
    }
    private void backtrack(int start, int n, int k, LinkedList<Integer> track){
        if (track.size() == k){
            res.add(new ArrayList<>(track));
            return;
        }
        for (int i = start; i <= n; i++){
            track.add(i);
            backtrack(i + 1, n, k, track);
            track.removeLast();
        }
    }
}

169.多数元素

class Solution {
    public int majorityElement(int[] nums) {
Arrays.sort(nums);
int n= nums.length;
int count=0;
int result=nums[0];
for(int i=1;i<n;i++){
if(nums[i]>nums[i-1]){
    count=0;
}
count++;
if(count>n/2){
    result= nums[i];
}
}
return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值