zoukankan      html  css  js  c++  java
  • leetcode题解

    1. 力扣21题 —— 合并两个有序链表

    public class MergerTwoLinkList {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
    
    public class ListNode {
        int val;
        ListNode next;
    
        public ListNode(int val) {
            this.val = val;
        }
    }
    }
    

    2. 力扣820 —— 单词的压缩编码

    public class CompressCoding {
    public int minimumLengthEncoding1(String[] words) {
    
        Set<String> set = new HashSet<>(Arrays.asList(words));
        for (String str : words) {
            for (int i = 1; i < str.length(); i++) {
                set.remove(str.substring(i));
            }
        }
    
        //利用set的流获取最后的总和
        return set.stream().mapToInt(x -> x.length() + 1).sum();
    }
    }
    

    3. 力扣20 —— 有效的括号

    public class EffectiveStr {
    Map<Character, Character> map;
    
    public EffectiveStr() {
        map = new HashMap<Character, Character>(3);
        map.put('}', '{');
        map.put(']', '[');
        map.put(')', '(');
    }
    
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                if (stack.isEmpty()) {
                    return false;
                }
                if (map.get(c) != stack.pop()) {
                    return false;
                }
            } else {
                stack.push(c);
            }
        }
        if (stack.isEmpty()) {
            return true;
        }
        return false;
    }
    }
    

    4. 力扣3 无重复字符的最长子串

    public class LongestSubStr {
    public int lengthOfLongestSubstring(String s) {
        if (s == "") {
            return 0;
        }
        int max = 0;
        List<Character> list = new ArrayList<>(s.length());
        for (int i = 0; i < s.length(); i++) {
            for (int j = i; j < s.length(); j++) {
                if (list.contains(s.charAt(j))) {
                    list.clear();
                    break;
                } else {
                    list.add(s.charAt(j));
                }
                if (max < list.size()) {
                    max = list.size();
                }
            }
            if (max < list.size()) {
                max = list.size();
            }
        }
        return max;
    }
    }
    

    5. 力扣2 —— 两数相加

    public class AddTwoNumByLinkList {
    static class ListNode {
        int val;
        ListNode next;
    
        public ListNode(int val) {
            this.val = val;
        }
    }
    
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // 先创建一个链表,第一个数值为0
        ListNode temp = new ListNode(0);
        ListNode next = temp;
        ListNode head1 = l1;
        ListNode head2 = l2;
        // 因为数值是按照个位数开始相加的,故最大是9+9 = 19 ,进位为1   carry 的值为 0或者 1
        int carry = 0;
        while (head1 != null || head2 != null) {
            int head11 = head1 == null ? 0 : head1.val;
            int head22 = head2 == null ? 0 : head2.val;
            next.next = new ListNode((head11 + head22 + carry) % 10);
            carry = (head11 + head22 + carry) / 10;
            head1 = head1.next;
            head2 = head2.next;
            next = next.next;
        }
        // 防止最后的进位  例如 9+9 = 18 ,有额外的进位18
        if (carry > 0) {
            next.next = new ListNode(carry);
        }
        return temp.next;
    }
    
    public static void display(ListNode listNode) {
        ListNode head = listNode;
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }
    }
    

    6. 力扣7 —— 整数反转

    public class RverseNum {
    public int reverse(int x) {
        int result;
        Stack<Character> stack = new Stack<>();
        String str = String.valueOf(x);
        for (int i = 0; i < str.length(); i++) {
            stack.push(str.charAt(i));
        }
        StringBuilder builder = new StringBuilder();
        while (!stack.empty()) {
            builder.append(stack.pop());
        }
        String strReverse = builder.toString();
        // 没有找到-号 说明是正数,直接判断有没有大于最大值  若没有直接返回
        if (strReverse.indexOf('-') == -1) {
            result = strToInt(strReverse);
        } else {
            strReverse = strReverse.substring(0, strReverse.length() - 1);
            strReverse = "-" + strReverse;
            result = strToInt(strReverse);
        }
    
        return result;
    }
    
    public int strToInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return 0;
        }
    }
    }
  • 相关阅读:
    算法(5)
    字典
    算法(4)
    AD域设置
    css两句话搞定漂亮表格样式
    Dev控件用法 aspxTreeList 无刷新 aspxGridView 数据
    ASP.Net 验证视图状态 MAC 失败
    C# 客服端上传文件与服务器器端接收 (简单代码)
    Linq to SQL 类型的对象图包含循环,如果禁用引用跟踪,择无法对其进行序列化。
    C# 导出 Excel 数字列出现‘0’的解决办法
  • 原文地址:https://www.cnblogs.com/cherrie-lin/p/14087474.html
Copyright © 2011-2022 走看看