Menu Sidebar
Menu

Delete the Middle Node of a Linked List

删掉中间的node.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteMiddle(ListNode head) {
        if(head == null || head.next == null)
            return null;
        if(head.next.next == null){
            head.next = null;
            return head;
        }
        ListNode dhead = new ListNode();
        dhead.next = head;
        ListNode slow = new ListNode();
        slow.next = head;
        ListNode fast = new ListNode();
        fast.next = head;
        while(fast.next != null && fast.next.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
        }
        slow.next = slow.next.next;
        return dhead.next;
    }
}

Finding 3-Digit Even Numbers

求所有三个字母的偶数, 并且由给的digits组成.

这个题的答案有范围, 而且很小, 一个个找就行了.

class Solution {
    public int[] findEvenNumbers(int[] digits) {
        int[] used = new int[10];
        for(int d : digits)
            used[d]++;
        List<Integer> list = new ArrayList<>();
        for(int i = 100; i <= 999; i++)
        {
            if(i % 2 != 0)
                continue;
            boolean yes = true;
            int[] cur = Arrays.copyOf(used, 10);
            String str = i+"";
            for(char c : str.toCharArray()){
                cur[c - '0'] --;
                if(cur[c - '0'] < 0){
                    yes = false;
                }
            }
            if(yes)
                list.add(i);
        }
        int[] res = new int[list.size()];
        for(int i = 0; i < list.size(); i++){
            res[i] = list.get(i); 
        }
        return res;
    }
}

Check if Numbers Are Ascending in a Sentence

class Solution {
    public boolean areNumbersAscending(String s) {
        String[] strs = s.split(" ");
        int n = -1;
        for(String ss : strs){
            if(isNum(ss)){
                int m = Integer.valueOf(ss);
                if(n >= m)
                    return false;
                n = m;
            }
        }
        return true;
    }
    
    public boolean isNum(String s)
    {
        for(char c : s.toCharArray())
            if(!Character.isDigit(c))
                return false;
        return true;
    }
}

Reverse Prefix of Word

给一个word, 给一个char, 求这个word开始到char的substring的反转后的结果.

class Solution {
    public String reversePrefix(String word, char ch) {
        char[] chs = word.toCharArray();
        int ind = word.indexOf(ch);
        if(ind == -1)
            return word;
        int i = 0;
        int j = ind;
        while(i < j){
            swap(chs, i++, j--);
        }
        return String.valueOf(chs);
    }
    public void swap(char[] c, int i, int j){
        char t = c[i];
        c[i] = c[j];
        c[j] = t;
    }
}

Count Number of Pairs With Absolute Difference K

给一个数组, 和一个数字k, 求数组中的pair的个数, 他们的绝对值差等于k.

这题就存一下数字, 然后因为数字大小范围是[1,200], 所以直接算一下这个范围可能的结果即可.

class Solution {
    public int countKDifference(int[] nums, int k) {
        int res = 0;
        Map<Integer, Integer> count = new HashMap<>();
        for(int n : nums)
            count.put(n, count.getOrDefault(n, 0) + 1);
        for(int i = 1; i <= 100 ; i++){
            int a = count.getOrDefault(i, 0);
            int b = count.getOrDefault(i + k,0);
            if(a != 0 && b != 0){
                res += a * b;
            }
        }
        return res;
    }
}

Smallest Greater Multiple Made of Two Digits

给三个数字, k,d1,d2. 求一个数组, 大于k, 是k的倍数,并且由d1和d2组成的最小数字.

这题就是一个个搜索d1和d2可以组成的数字, 注意一下边界条件.

class Solution {
    public int findInteger(int k, int d1, int d2) {
        return find(k,d1,d2, 0);
    }
    public int find(int k, int d1, int d2, long cur){
        if(cur >= Integer.MAX_VALUE)
            return -1;
        if(cur > k && cur % k == 0)
            return (int)cur;
        int n1 = cur + d1 == 0 ? -1 : find(k, d1, d2, cur*10+d1);
        int n2 = cur + d2 == 0 ? -1 : find(k, d1, d2, cur*10+d2);
        if(n1 != -1 && n2 != -1)
            return Math.min(n1,n2);
        else if(n1 != -1)
            return n1;
        else
            return n2;
    }
}

Flip Game II

设定一个游戏, 翻转字符串的任意++变成–, 问开始玩的人是否一定能赢.

这题就是记忆搜索的模板题, 用一个memo存字符串改变后的状态的答案, 然后按个找结果.

class Solution {
    public boolean canWin(String s) {
        Map<String, Boolean> memo = new HashMap<>();
        find(s, memo);
        return memo.get(s);
    }
    boolean find(String s, Map<String, Boolean> memo){
        if(memo.containsKey(s))
            return memo.get(s);
        Boolean res = false;
        for(int i = 0; i < s.length(); i++) {
            if(i > 0 && s.charAt(i) == '+' && s.charAt(i - 1) == '+'){
                StringBuffer sb = new StringBuffer(s);
                sb.setCharAt(i - 1, '-');
                sb.setCharAt(i, '-');
                if(!find(sb.toString(),memo)){
                    res = true;
                    break;
                }
            }
        }
        memo.put(s, res);
        return res;
    }
}

Find Greatest Common Divisor of Array

给一个数组, 求里面最大值和最小值的gcd.

class Solution {
    public int findGCD(int[] nums) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int n : nums){
            max = Math.max(max, n); 
            min = Math.min(min, n);
        }
        return gcd(min, max);
    }
    public int gcd(int a, int b){
        if(a == 0)
            return b;
        return gcd(b % a, a);
    }
}

Find the Middle Index in Array

定义middleindex是一个index的左右两边的sum都一样(不包含自己), 找到最左边的middle index.

这题就是做下presum, 然后找.

class Solution {
    public int findMiddleIndex(int[] nums) {
        int rightSum = 0;
        for(int i = 1; i < nums.length; i++)
            rightSum += nums[i];
        int leftSum = 0;
        for(int i = 0; i < nums.length; i++){
            if(leftSum == rightSum)
                return i;
            leftSum += nums[i];
            if(i < nums.length - 1)
                rightSum -= nums[i + 1];
        }
        return -1;
    }
}
Newer Posts
Older Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

February 2025
M T W T F S S
 12
3456789
10111213141516
17181920212223
2425262728