暑假刷题训练营打卡7.31

@心海 2022-07-31 22:39:01

第一题

/**
 * 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 boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode slow = head, fast = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode next = slow.next;
        slow.next = null;
        while (next != null) {
            pre = slow;
            slow = next;
            next = next.next;
            slow.next = pre;
        }
        ListNode newHead = slow;
        while (head != null && newHead != null) {
            if (head.val != newHead.val) {
                return false;
            }
            newHead = newHead.next;
            head = head.next;
        }
        return true;  
    }
}

第二题

class Solution {
    public List<List<String>> partition(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n + 2][n + 2];
        int[] tmp = new int[(n + 2) * (n + 2)];
        int cnt = 0;
        List<List<String>> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                if (i == j) {
                    dp[i][j] = true;
                } else {
                    if (s.charAt(i) == s.charAt(j)) {
                        if (i==j+1){
                            dp[i][j] = true;
                        }else {
                            dp[i][j] = dp[i - 1][j + 1];
                        }
                    } else {
                        dp[i][j] = false;
                    }
                }
                if (dp[i][j]) {
                    tmp[cnt] = i * 100 + j;
                    cnt++;
                }
            }
        }
        List<Set<String>> x = new ArrayList<>();
        x.add(new HashSet<>());
        x.get(0).add(s.charAt(0) + "#" + 0);
        for (int i = 1; i < cnt; i++) {
            int l = tmp[i] % 100;
            int r = tmp[i] / 100;
            Set<String> iSet = new HashSet<>();
            x.add(iSet);
            String tmpS = s.substring(l, r + 1);
            for (int j = 0; j < i; j++) {
                Set<String> y = x.get(j);
                if (l == 0) {
                    iSet.add(tmpS + "#" + r);
                }else {
                    for (String z : y) {
                        int hasr = Integer.parseInt(z.split("#")[1]);
                        if (l == hasr + 1) {
                            iSet.add(z.replace("#" + hasr, "") + "," + tmpS + "#" + r);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < cnt; i++) {
            Set<String> y = x.get(i);
            for (String z : y) {
                if (Integer.parseInt(z.split("#")[1]) == n - 1) {
                    String[] xx = z.replace("#" + (n - 1), "").split(",");
                    List<String> yy = new ArrayList<>();
                    for (int j = 0; j < xx.length; j++) {
                        yy.add(xx[j]);
                    }
                    ans.add(yy);
                }
            }
        }
        return ans;
    }
}

 

第三题

class Solution {
    public int primePalindrome(int n) {
        while (true) {
            if (n == reverse(n) && isPrime(n))
                return n;
            n++;
            if (10_000_000 < n && n < 100_000_000)
                n = 100_000_000;
        }
    }
    public boolean isPrime(int n) {
        if (n < 2) return false;
        int R = (int) Math.sqrt(n);
        for (int d = 2; d <= R; ++d)
            if (n % d == 0) return false;
        return true;
    }
    public int reverse(int n) {
        int ans = 0;
        while (n > 0) {
            ans = 10 * ans + (n % 10);
            n /= 10;
        }
        return ans;
    }
}

 

第四题

 

 

...全文
46 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文介绍了基于粒子群优化算法(PSO)在IEEE33节点配电系统中进行故障定位及故障区段判定的研究,结合Matlab代码实现,旨在利用智能优化算法提高电力系统故障检测的准确性与效率。文中详细阐述了故障定位的数学模型构建、适应度函数设计以及粒子群算法的参数设置与迭代优化过程【故障定位】基于粒子群优化算法的故障定位及故障区段研究【IEEE33节点】(Matlab代码实现),并通过IEEE33节点标准测试系统验证了方法的有效性与鲁棒性。该研究为配电网自动化与智能化运维提供了技术支持。; 适合人群:电气工程、电力系统自动化、智能优化算法等相关专业的研究生、科研人员及从事电力系统故障诊断的工程技术人员。; 使用场景及目标:①应用于配电网故障快速定位与隔离,提升供电可靠性;②作为智能优化算法在电力系统中应用的教学案例,帮助理解PSO算法的实际建模与实现流程;③为后续研究如多目标故障定位、含分布式电源的复杂配网故障分析提供技术参考。; 阅读建议:建议读者结合提供的Matlab代码,深入理解算法实现细节,重点关注适应度函数的设计逻辑与IEEE33节点系统的数据结构处理,同时可尝试调整算法参数以观察性能变化,进一步掌握智能算法在电力系统中的调优方法。

51,684

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧