一些算法编程题

目录

反转链表

素数个数统计

两数相加



记录一些刷过的题目。。。

反转链表

public class ReverseListDemo {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1, null);
        ListNode node2 = new ListNode(2, node1);
        ListNode node3 = new ListNode(3, node2);
        Solution solution = new Solution();
//        ListNode reverseList = solution.reverseListByIteration(node3);
        ListNode reverseList = solution.reverseListByRecursion(node3);
//        可用debug查看reverseList
        System.out.println(reverseList);
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    static class Solution {
        /**
         * 迭代
         *
         * @param head 链表
         * @return 反转后的链表
         */
        ListNode reverseListByIteration(ListNode head) {
            ListNode prev = null;
            ListNode curr = head;
            while (curr != null) {
                ListNode next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next;
            }
            return prev;
        }

        /**
         * 递归
         *
         * @param head 链表
         * @return 反转后的链表
         */
        ListNode reverseListByRecursion(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode newNode = reverseListByRecursion(head.next);

            head.next.next = head;
            head.next = null;

            return newNode;
        }
        
    }
}

素数个数统计

public class PrimeNumCountDemo {
    public static void main(String[] args) {
//        System.out.println(forceCount(100));
        System.out.println(eratosthenes(100));
    }

    // 暴力求解法
    private static int forceCount(int max) {
        if (max < 2) {
            return 1;
        }
        int count = 0;
        for (int i = 2; i < max; i++) {
            count += isPrime(i) ? 1 : 0;
        }
        return count;
    }

    private static boolean isPrime(int value) {
        for (int i = 2; i < value / 2 + 1; i++) {
            if (value % i == 0) {
                return false;
            }
        }
        return true;
    }

    // 埃式筛选法
    private static int eratosthenes(int n) {
        boolean[] isPrime = new boolean[n];
        int count = 0;
        for (int i = 2; i < n; i++) {
            if (!isPrime[i]) {
                count++;
                for (int j = 2 * i; j < n; j += i) {
                    isPrime[j] = true;
                }
            }
        }
        return count;
    }
}

两数相加

/**
 * 给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
 * 请你将两个数相加,并以相同形式返回一个表示和的链表。
 * 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + carry;
            carry = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);
            cur = cur.next;
            if (l1 != null)
                l1 = l1.next;
            if (l2 != null)
                l2 = l2.next;
        }
        if (carry == 1) {
            cur.next = new ListNode(carry);
        }
        return pre.next;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode l1 = new ListNode(2);
        l1.next = new ListNode(4);
        l1.next.next = new ListNode(3);
        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);
        ListNode listNode = solution.addTwoNumbers(l1, l2);
        System.out.println(listNode.val);
        System.out.println(listNode.next.val);
        System.out.println(listNode.next.next.val);

/*        ListNode prev = new ListNode(0);
        ListNode cur = prev;
        cur.next = new ListNode(1);
        System.out.println(prev.next.val);*/


    }


}

无重复字符的最长子串

/**
 * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。如:abcabc应返回3,bbbb返回1,pwwkew返回3
 */
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0)
            return 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            max = Math.max(max, i + 1 - left);
        }
        return max;
    }
}

最长回文子串



/**
 * 给你一个字符串 s,找到 s 中最长的回文子串。
 * 如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。
 */
public class Solution {
    public String longestPalindrome(String s) {
        if (s == null || s.length() < 1) {
            return "";
        }
        int start = 0, end = 0;
        for (int i = 0; i < s.length(); i++) {
            int len1 = expandAroundCenter(s, i, i);
            int len2 = expandAroundCenter(s, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        return s.substring(start, end + 1);
    }

    public int expandAroundCenter(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            --left;
            ++right;
        }
        return right - left - 1;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String subStr = solution.longestPalindrome("abcabacbcace");
        System.out.println(subStr);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏舟飞流

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值