咸宁 网站建设,宿迁发布最新通告,收录好的网站有哪些,vps上的网站运行太慢替换子串得到平衡字符串
题目描述
有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符#xff0c;且长度为 n 的字符串。
假如在该字符串中#xff0c;这四个字符都恰好出现 n/4 次#xff0c;那么它就是一个「平衡字符串」。
给你一个这样的字符串 s#xff0c;请通过…替换子串得到平衡字符串
题目描述
有一个只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符且长度为 n 的字符串。
假如在该字符串中这四个字符都恰好出现 n/4 次那么它就是一个「平衡字符串」。
给你一个这样的字符串 s请通过「替换一个子串」的方式使原字符串 s 变成一个「平衡字符串」。
你可以用和「待替换子串」长度相同的 任何 其他字符串来完成替换。
请返回待替换子串的最小可能长度。
如果原字符串自身就是一个平衡字符串则返回 0。
样例
样例输入 s “QWER” s “QQWE” s “QQQW” s “QQQQ” 样例输出 0 s 已经是平衡的了。 1 我们需要把一个 ‘Q’ 替换成 ‘R’这样得到的 “RQWE” (或 “QRWE”) 是平衡的。 2 我们可以把前面的 “QQ” 替换成 “ER”。 3 我们可以替换后 3 个 ‘Q’使 s “QWER”。 提示
1s.length1051 s.length 10^51s.length105s.length 是 4 的倍数s 中只含有 ‘Q’, ‘W’, ‘E’, ‘R’ 四种字符
思路
n的范围为1e5需要设计的算法的时间复杂度不能大于O(n2)O(n^2)O(n2)。测试用例有点怪。题目理解起来比较麻烦。 看了题解。使用的是双指针。
代码实现
class Solution {public int balancedString(String s) {int len s.length();int standard len / 4;int[] arr new int[Z];for(int i 0; i len; i) arr[s.charAt(i)];if(arr[Q] standard arr[W] standard arr[E] standard arr[R] standard) return 0;int ans len;for(int left 0, right 0; right len; right){--arr[s.charAt(right)];while(arr[Q] standard arr[W] standard arr[E] standard arr[R] standard){ans Math.min(ans, right - left 1);arr[s.charAt(left)];}}return ans;}
}课程表
题目描述
你这个学期必须选修 numCourses 门课程记为 0 到 numCourses - 1 。
在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出其中 prerequisites[i] [ai, bi] 表示如果要学习课程 ai 则 必须 先学习课程 bi 。
例如先修课程对 [0, 1] 表示想要学习课程 0 你需要先完成课程 1 。 请你判断是否可能完成所有课程的学习如果可以返回 true 否则返回 false 。
样例
样例输入 numCourses 2, prerequisites [[1,0]] numCourses 2, prerequisites [[1,0],[0,1]] 样例输出 true 总共有 2 门课程。学习课程 1 之前你需要完成课程 0 。这是可能的。 false 总共有 2 门课程。学习课程 1 之前你需要先完成课程 0 并且学习课程 0 之前你还应先完成课程 1 。这是不可能的。 提示
1numCourses1051 numCourses 10^51numCourses1050 prerequisites.length 5000prerequisites[i].length 20 ai, bi numCoursesprerequisites[i] 中的所有课程对 互不相同
思路
拓扑排序刚好想写一些拓扑排序的题目这题应该就是拓扑排序的基本。给出每个点的入度只需返回能否完成拓扑排序即图中是否存在自环。dfs、bfs均可使用。
代码实现
dfs
class Solution {ListListInteger edges;public boolean canFinish(int numCourses, int[][] prerequisites) {edges new ArrayList();int[] vis new int[numCourses];for(int i 0; i numCourses; i) edges.add(new ArrayList());for(int[] cur : prerequisites) edges.get(cur[1]).add(cur[0]);for(int i 0; i numCourses; i)if(!dfs(vis, i)) return false;return true;}// vis状态为1表示当此进行dfs时已经遍历过再次遇到属于环。即不满足拓扑排序// vis状态为-1表示之前dfs已经遍历过当前结点相当于给dfs剪枝吧。private boolean dfs(int[] vis, int index){if(vis[index] 1) return false;if(vis[index] -1) return true;vis[index] 1;for(int i : edges.get(index)){if(!dfs(vis, i)) return false;}vis[index] -1;return true;}
}bfs
class Solution {public boolean canFinish(int numCourses, int[][] prerequisites) {int[] out new int[numCourses];ListListInteger edges new ArrayList();for(int i 0; i numCourses; i) edges.add(new ArrayListInteger());QueueInteger queue new ArrayDeque();for(int[] prerequisit : prerequisites){out[prerequisit[0]];edges.get(prerequisit[1]).add(prerequisit[0]);}for(int i 0; i numCourses; i) if(out[i] 0) queue.offer(i);while(!queue.isEmpty()){int pre queue.poll();numCourses--;for(int cur : edges.get(pre)){if(--out[cur] 0) queue.offer(cur);}}return numCourses 0;}
}
文章转载自: http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.bntfy.cn.gov.cn.bntfy.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.pkdng.cn.gov.cn.pkdng.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.ztcwp.cn.gov.cn.ztcwp.cn http://www.morning.ttdbr.cn.gov.cn.ttdbr.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.gwsdt.cn.gov.cn.gwsdt.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.fjglf.cn.gov.cn.fjglf.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.drrt.cn.gov.cn.drrt.cn