算法刷题记录(LeetCode 151-180)

154. Find Minimum in Rotated Sorted Array II

class Solution {
    public int findMin(int[] nums) {
        int left=0;
        int right=nums.length-1;
        while (left<right){
            int mid=(left+right)>>1;
            if (nums[mid]<nums[right]){
                right = mid;
            }
            else if (nums[mid]>nums[right]){
                left = mid+1;
            }
            else {
                right--;
            }
        }
        return nums[left];
    }
}

166. Fraction to Recurring Decimal

    public String fractionToDecimal(int numerator, int denominator) {
        long a = numerator, b = denominator;
        StringBuilder resString=new StringBuilder();
        if (a%b==0){
            return String.valueOf(a/b);
        }
        if (a*b<0){
            resString.append("-");
            a=Math.abs(a);
            b=Math.abs(b);
        }
        StringBuilder remainder =new StringBuilder();
        HashMap<Long,Integer> map=new HashMap<>();
        // 计算小数点前的部分,并将余数赋值给 a
        resString.append(a/b);
        resString.append(".");
        a=a%b;
        while (a!=0){
            map.put(a,resString.length());
            a*=10;
            resString.append(a/b);
            a%=b;
            if (map.containsKey(a)){
                return String.format("%s(%s)",resString.substring(0,map.get(a)),resString.substring(map.get(a)));
            }
        }
        return resString.append(remainder).toString();
    }

167. Two Sum II - Input Array Is Sorted

    public int[] twoSum(int[] numbers, int target) {
        int left=0;
        int right=numbers.length-1;
        while (left<right){
            int currSum=numbers[left]+numbers[right];
            if (currSum>target){
                right--;
            }
            else if (currSum<target){
                left++;
            }
            else {
                return new int[]{left+1,right+1};
            }
        }
        return null;
    }

171. Excel Sheet Column Number

    public int titleToNumber(String columnTitle) {
        int carry=1;
        int res=0;
        for (int i=columnTitle.length()-1;i>=0;i--){
            res+=(columnTitle.charAt(i)-'A'+1)*carry;
            carry*=26;
        }
        return res;
    }

172. Factorial Trailing Zeroes

    public int trailingZeroes(int n) {
        int base=5;
        int fives=0;
//        int coefficient=1;
        while (base<=n){
            fives+=n/base;
            base*=5;
        }
        return fives;
    }

*173. Binary Search Tree Iterator

class BSTIterator {
    private final Stack<TreeNode> nodeStack;
    public BSTIterator(TreeNode root) {
        nodeStack=new Stack<>();
        TreeNode curr=root;
        while (curr!=null){
            nodeStack.push(curr);
            curr=curr.left;
        }
    }

    public int next() {
        TreeNode curr=nodeStack.pop();
        int val=curr.val;
        if (curr.right!=null){
            curr=curr.right;
            while (curr!=null){
                nodeStack.push(curr);
                curr=curr.left;
            }
        }
        return val;
    }

    public boolean hasNext() {
        return !nodeStack.isEmpty();
    }
}

*174. Dungeon Game

    public int calculateMinimumHP(int[][] dungeon) {
        int[][] dp=new int[dungeon.length+1][dungeon[0].length+1];
        for (int i=0;i<=dungeon.length;i++){
            dp[i][dungeon[0].length]=Integer.MAX_VALUE;
        }
        for (int i=0;i<=dungeon[0].length;i++){
            dp[dungeon.length][i]=Integer.MAX_VALUE;
        }
        dp[dungeon.length-1][dungeon[0].length]=1;
        dp[dungeon.length][dungeon[0].length-1]=1;
        for (int i=dungeon.length-1;i>=0;i--){
            for (int j=dungeon[0].length-1;j>=0;j--){
                int need=Math.min(dp[i+1][j],dp[i][j+1])-dungeon[i][j];
                need = need<=0?1:need;
                dp[i][j]=need;
            }
        }
        return dp[0][0];
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值