头像

xiaozuo

塔利班组织




离线:54分钟前


活动打卡代码 AcWing 1453. 移掉K位数字

xiaozuo
2天前
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int cnt = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        sb.append("0");
        for(int i =0 ;i<str.length();i++){
            while(cnt >0 && str.charAt(i) < sb.charAt(sb.length()-1) ){
                sb.deleteCharAt(sb.length() -1);
                cnt-- ;
            }
            sb.append(str.charAt(i));
        }
        while(cnt >0){
            cnt -- ;
            sb.deleteCharAt(sb.length()-1);
        }
        int step = 0 ;
        while(step < sb.length() && sb.charAt(step) == '0') step++;

        if(step == sb.length()) System.out.println("0");
        else System.out.println(sb.delete(0,step));
    }
}


活动打卡代码 LeetCode 179. 最大数

xiaozuo
3天前
class Solution {
    public String largestNumber(int[] nums) {
        if(nums==null || nums.length==0) return "";
        String[] strs = new String[nums.length];
        for(int i =0;i<nums.length;i++){
            strs[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(strs,(o1,o2)->(o1+o2).compareTo(o2+o1));
        StringBuilder sb = new StringBuilder();
        for(int i =strs.length-1;i>=0;i--){
            sb.append(strs[i]);
        }
        if(sb.charAt(0) == '0') return "0";
        return sb.toString();
    }
}



xiaozuo
3天前
class Solution {
    public int getNumberSameAsIndex(int[] nums) {
        int l = 0,r = nums.length - 1;
        while(l<r){
            int mid = l+r >>1;
            if(nums[mid] >= mid) r = mid;
            else l = mid+1;
        }
        if(r == nums[r]) return r;
        return -1;
    }
}



xiaozuo
3天前
class Solution {
    public int trap(int[] height) {
        /*
        只要右边有比左边高的柱子,无论中间是什么情况。
        当前能存的雨水量只和左边的柱子相关。
        因此 ans +=max(0,left_max-height[left]); 

        同理,如果左边比右边的柱子高,无论中间是什么情况,
        当前能存的水量为 ans +=max(0,right_max-height[right]);

        左右两个指针更新左边最高和右边最高,直到相交*/

        int left = 0;
        int right = height.length - 1;
        int left_max = 0;
        int right_max = 0;
        int ans = 0;

        while(left <= right){
            ///只要右边有比左边高,无论中间是什么情况,当前所能存的雨水只和左边的最高相关.
            if(left_max < right_max){
                ans = ans + Math.max(0,left_max-height[left]);
                left_max = Math.max(left_max,height[left]);
                left++;
            }else{
                ans = ans + Math.max(0,right_max-height[right]);
                right_max = Math.max(right_max,height[right]);
                right--;
            }
        }
        return ans;
    }
}


活动打卡代码 AcWing 62. 丑数

xiaozuo
3天前
class Solution {
    public int getUglyNumber(int n) {
        if(n == 1) return 1;
        List<Integer> list = new ArrayList<>();
        int i = 0;
        int j = 0;
        int k = 0;
        list.add(1);
        while(list.size() < n){
            int res = Math.min(Math.min(list.get(i)*2,list.get(j)*3),list.get(k)*5);

            list.add(res);
            if(list.get(i)*2 == res) i++;
            if(list.get(j)*3 == res) j++;
            if(list.get(k)*5 == res) k++;
        }
        return list.get(list.size()-1);
    }
}


活动打卡代码 LeetCode 263. 丑数

xiaozuo
3天前
class Solution {
    public boolean isUgly(int n) {
        if(n == 0) return false;

        while(n!=1){
            boolean flag = false;
            if(n%2 ==0){
                n /= 2;
                flag = true;
            }else if(n%5 ==0){
                n /= 5;
                flag = true;
            }else if(n%3 ==0){
                n /= 3;
                flag = true;
            }
            if(!flag){
                return false;
            }
        }
        return true;
    }
}



xiaozuo
3天前
class Solution {
    public int findMin(int[] nums) {
        int l = 0,r = nums.length - 1;
        while(l < r&& nums[r] == nums[0]) r--;
        if(nums[l] <= nums[r]) return nums[0];
        while(l<r){
            int mid = l+r>>1;
            if(nums[mid] < nums[0]) r = mid;
            else l = mid+1;
        }
        return nums[r];
    }
}



xiaozuo
6天前
class Solution {
    public int getMissingNumber(int[] nums) {
        if(nums.length == 0) return 0;
        int l =0,r = nums.length-1;
        while(l < r){
            int m  = (l+r)>>1;
            if(nums[m] == m){
                l = m+1;
            }else{
                r = m;
            }
        }
        // System.out.println(r);
        if(nums[r] == r) return ++r;
        return r;
    }
}



xiaozuo
7天前
class Solution {
    public int findMin(int[] nums) {
        int n = nums.length-1;
        if(nums[0] <= nums[n]) return nums[0];
        int l = 0,r = n;
        while(l < r){
            int m = l + (r-l)/2;
            // [l,m] [m+1,r]
            if(nums[m] < nums[r]) r = m;
            else l = m+1;
        }
        return nums[r];
    }
}



xiaozuo
7天前
class Solution {
    public boolean search(int[] arr, int target) {
        int len = arr.length;
        if (len < 1) return false;
        int l = 0, r = len - 1;
        while (l <= r) {
            int mid = l + r >> 1;
            if (arr[mid] == target) return true;
            else if (arr[mid] > arr[l]) {
                //说明l ~ mid非严格单调递增
                if (target >= arr[l] && target < arr[mid]) r = mid - 1;
                else l = mid + 1;
            } else if (arr[mid] < arr[l]){
                //说明mid ~ r非严格单调递增
                if (target > arr[mid] && target <= arr[r]) l = mid + 1;
                else r = mid - 1;
            } else l++;
        }
        return false;
    }
}