LeetCode Contest 130

LeetCode第130周周赛问题基本不难。
1017. Convert to Base -2
这个问题类似于十进制求二进制表达的过程,但因为是负数,需要做一些处理,数学证明这里不赘述,参考

class Solution {
    public String baseNeg2(int N) {
    
        int remainder=N%(-2);
        N/=(-2);
        
        if(remainder<0)
        {
            remainder+=2;
            N++;
        }
        
        if(N==0)
            return String.valueOf(remainder);
        return baseNeg2(N)+String.valueOf(remainder);
        
    }
}

更为一般的结论代码:

public static String toNegativeBase(int n, int negBase) 
{ 
    //  If n is zero then in any base it will be 0 only 
        int remainder=N%(negBase);
        N/=(negBase);
        
        if(remainder<0)
        {
            remainder-=negBase;
            N++;
        }
        
        if(N==0)
            return String.valueOf(remainder);
        return toNegativeBase(N)+String.valueOf(remainder);
} 

1018. Binary Prefix Divisible By 5
这个问题不大,就是利用余数性质来避免整数溢出

class Solution {
    public List<Boolean> prefixesDivBy5(int[] A) {
        
        LinkedList<Boolean> ret=new LinkedList<>();
        int tmp=0;
        for(int i=0;i<A.length;i++)
        {
            tmp=(tmp<<1 | A[i])%5;
            if(tmp%5==0)
                ret.add(true);
            else
                ret.add(false);
        }
        return ret;
    }
}

1019. Next Greater Node In Linked List

我用的解法很一般,十分暴力。

class Solution {
    
    public int[] nextLargerNodes(ListNode head) {
    
        ArrayList<Integer> ret=new ArrayList<>();
        
        ListNode p=head;
        while(p!=null)
        {
            ListNode q=p.next;
            while(q!=null && q.val<=p.val)
                q=q.next;
            if(q==null)
                ret.add(0);
            else
                ret.add(q.val);
            p=p.next;
        }
        int[] res=new int[ret.size()];
        for(int i=0;i<ret.size();i++)
            res[i]=ret.get(i);
        
        return res;
    }
}

看到一种很妙的解法,链表递归加栈真的很强,时间复杂度仅为O(N):

class Solution {
    
    public static Stack<Integer> stack;
    
    public static int[] ret;
    
    public int[] nextLargerNodes(ListNode head) {
    
        stack=new Stack<>();
        recursive(head,0);
        return ret;
        
    }
    
    
    public static void recursive(ListNode node,int idx)
    {
        if(node==null)
        {
            ret=new int[idx];
            return;
        }
        recursive(node.next,idx+1);
        while(!stack.isEmpty() && stack.peek()<=node.val)
            stack.pop();
        if(stack.isEmpty())
            ret[idx]=0;
        else
            ret[idx]=stack.peek();
        stack.push(node.val);
    }
}

1020. Number of Enclaves
这一问中规中矩,正常的格网递归搜索即可

class Solution {
    
    public static int n_row;
    
    public static int n_col;
    
    public int numEnclaves(int[][] A) {
        
        int ret=0;
        n_row=A.length;
        
        n_col=A[0].length;
        
        for(int j=0;j<n_col;j++)
        {
            if(A[0][j]==1)
            {
                dfs(0,j,A);
            } 
            if(A[n_row-1][j]==1)
            {
                dfs(n_row-1,j,A);
            }
        }
        
        for(int i=0;i<n_row;i++)
        {
            if(A[i][0]==1)
            {
                dfs(i,0,A);
            }
            if(A[i][n_col-1]==1)
            {
                dfs(i,n_col-1,A);
            }
        }
        
        for(int i=0;i<n_row;i++)
        {
            for(int j=0;j<n_col;j++)
            {
                if(A[i][j]==1)
                    ret++;
            }
        }
        return ret;
    }
    
    public static void dfs(int i,int j,int[][] grid)
    {
        grid[i][j]=-1;
        for(int dx=-1;dx<=1;dx++)
        {
            for(int dy=-1;dy<=1;dy++)
            {
                if(Math.abs(dx+dy)==1 && validPos(i+dx,j+dy,grid))
                    dfs(i+dx,j+dy,grid);
            }
        }
    }
    
    public static boolean validPos(int i,int j,int[][] grid)
    {
        if(i<0 || i>=n_row || j<0 || j>=n_col || grid[i][j]!=1)
            return false;
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值