LeetCode, without it, only hands-on familiarity (2)

Hello everyone, I do 方圆
n’t have it, but I am familiar with it


Fast and slow pointer traversal

141. Circular Linked List

public class Solution {
    
    
    public boolean hasCycle(ListNode head) {
    
    
        if(head == null || head.next == null)
            return false;
        
        ListNode slow = head,fast = head.next;
        while(slow != fast){
    
    
            if(fast == null || fast.next == null)
                return false;
            slow = slow.next;
            fast = fast.next.next;
        }

        return true;
    }
}

202. Happy Number

class Solution {
    
    
    public boolean isHappy(int n) {
    
    
        int slow = n, fast = getNext(n);

        while(fast != 1) {
    
    
            if(slow == fast) return false;
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }

        return true;
    }

    private int getNext(int n) {
    
    
        int sum = 0;
        while(n != 0) {
    
    
            int x = n % 10;
            sum += x * x;
            n /= 10;
        }

        return sum;
    }
}

876. The middle node of the linked list

class Solution {
    
    
    public ListNode middleNode(ListNode head) {
    
    
        ListNode slow = head, fast = head;

        while(fast != null && fast.next != null) {
    
    
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }
}

Interval merge

56. Consolidation interval

class Solution {
    
    
    public int[][] merge(int[][] intervals) {
    
    
        List<int[]> res = new ArrayList<>();

        if(intervals == null || intervals.length == 0)
            return res.toArray(new int[0][]);
        
        Arrays.sort(intervals,(x,y) -> x[0] - y[0]);
        for(int i = 0; i < intervals.length; i++) {
    
    
            int left = intervals[i][0];
            int right = intervals[i][1];

			//注意此处是大于等于
            while(i < intervals.length - 1 && right >= intervals[i + 1][0]) {
    
    
                i++;
                right = Math.max(right,intervals[i][1]);
            }
            res.add(new int[] {
    
    left,right});
        }

        return res.toArray(new int[0][]);
    }
}

String manipulation

6. Zigzag transformation

//技巧性挺强的
class Solution {
    
    
    public String convert(String s, int numRows) {
    
    
        if(numRows == 1) return s;

        int len = Math.min(s.length(),numRows);
        String[] rows = new String[len];
        for(int i = 0; i < rows.length; i++) {
    
    
            rows[i] = "";
        }
        boolean down = true;
        int temp = 0;
        for(int j = 0; j < s.length(); j++) {
    
    
            if(down) rows[temp++] += s.substring(j,j + 1);
            else rows[temp--] += s.substring(j,j + 1);
            if(temp == 0 || temp == len - 1) down = !down;
        }
        String res = "";
        for(String str : rows) res += str;

        return res;
    }
}

14. Longest common prefix

class Solution {
    
    
    public String longestCommonPrefix(String[] strs) {
    
    
        if(strs == null || strs.length == 0) return "";

        String res = strs[0];
        for(int i = 1; i < strs.length; i++) {
    
    
            int j = 0;
            for(; j < res.length() && j < strs[i].length(); j++)
                if(res.charAt(j) != strs[i].charAt(j)) break;
            res = res.substring(0,j);
            if(res.equals("")) return "";
        }

        return res;
    }
}

763. Divide the letter interval

//这个技巧性也挺强的
class Solution {
    
    
    public List<Integer> partitionLabels(String S) {
    
    
        List<Integer> res = new ArrayList<>();

        int[] last = new int[26];
        for(int i = 0; i < S.length(); i++) {
    
    
            last[S.charAt(i) - 'a'] = i;
        }
        int head = 0,tail = 0;
        for(int i = 0; i < S.length(); i++) {
    
    
            tail = Math.max(tail,last[S.charAt(i) - 'a']);
            if(i == tail) {
    
    
                res.add(tail - head + 1);
                head = tail + 1;
            }
        }

        return res;
    }
}

Digital operation

7. Integer inversion

//越界判断
class Solution {
    
    
    public int reverse(int x) {
    
    
        int res = 0;
        int bundry = Integer.MAX_VALUE / 10;

        while(x != 0) {
    
    
            int n = x % 10;
            x /= 10;
            if(Math.abs(res) > bundry || (res == bundry && n > 7))
                return 0;
            res = res * 10 + n;
        }

        return res;
    }
}

8. String Conversion

class Solution {
    
    
    public int myAtoi(String str) {
    
    
        int res = 0;
        str = str.trim();
        if(str.length() == 0) return 0;

        boolean flag = true;
        int cur = 0;
        if(str.charAt(0) == '-') {
    
    
            flag = false;
            cur = 1;
        }else if(str.charAt(0) == '+') {
    
    
            cur = 1;
        }
        int bundry = Integer.MAX_VALUE / 10;
        for(int i = cur; i < str.length(); i++) {
    
    
            char n = str.charAt(i);
            if(n > '9' || n < '0') break;
            if(res > bundry || (res == bundry && n > '7'))
                return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            res = res * 10 + (n - '0');
        }

        return flag ? res : -res;
    }
}

9. Number of palindrome

class Solution {
    
    
    public boolean isPalindrome(int x) {
    
    
        if(x < 0) return false;

        int rex = 0,temp = x;
        while(temp != 0) {
    
    
            int n = temp % 10;
            temp /= 10;
            rex = rex * 10 + n;
        }

        if(rex == x) return true;
        else return false;
    }
}

43. String multiplication

class Solution {
    
    
    public String multiply(String num1, String num2) {
    
    
        if(num1.equals("0") || num2.equals("0")) return "0";

        int[] res = new int[num1.length() + num2.length()];
        for(int i = num1.length() - 1; i >= 0 ; i--) {
    
    
            int n1 = num1.charAt(i) - '0';
            for(int j = num2.length() - 1; j >= 0; j--) {
    
    
                int n2 = num2.charAt(j) - '0';
                int sum = res[i + j + 1] + n1 * n2;
                res[i + j + 1] = sum % 10;
                res[i + j] += sum / 10;
            }
        }
        StringBuilder ans = new StringBuilder();
        for(int i = 0; i < res.length; i++) {
    
    
            if(i == 0 && res[0] == 0) continue;
            ans.append(res[i]);
        }

        return ans.toString();
    }
}

172. Zero after factorial

class Solution {
    
    
    public int trailingZeroes(int n) {
    
    
        int count = 0;

        while(n > 0){
    
    
            count += n / 5;
            n /= 5;
        }
        return count;
    }
}

258. Everyone add up

class Solution {
    
    
    public int addDigits(int num) {
    
    
        if(num < 10) return num;

        int next = 0;
        while(num != 0){
    
    
            next += num % 10;
            num /= 10;
        }

        return addDigits(next);
    }
}

Come on! ! !

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/108134276