网站建设后怎么,昆明建站网站资讯平台,wordpress多页面主题,联谊会建设网站找出字符串中第一个匹配项的下标、求解方程----2023/5/2 
给你两个字符串 haystack 和 needle #xff0c;请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标#xff08;下标从 0 开始#xff09;。如果 needle 不是 haystack 的一部分#xff0c;则返回 -1…找出字符串中第一个匹配项的下标、求解方程----2023/5/2 
给你两个字符串 haystack 和 needle 请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标下标从 0 开始。如果 needle 不是 haystack 的一部分则返回 -1 。 示例1 
输入haystack  sadbutsad, needle  sad
输出0
解释sad 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 所以返回 0 。示例 2 
输入haystack  leetcode, needle  leeto
输出-1
解释leeto 没有在 leetcode 中出现所以返回 -1 。题解 
class Solution:def strStr(self, haystack: str, needle: str) - int:if len(haystack)  len(needle) and haystack  needle:return 0for index in range(len(haystack) - len(needle)1):if haystack[index:indexlen(needle)]  needle:return indexreturn -1题解KMP算法 参考 
class Solution:def strStr(self, haystack: str, needle: str) - int:n  len(haystack)m  len(needle)next  [0]k  0for i in range(1, m):while k  0 and needle[k] ! needle[i]:k  next[k-1]if needle[k]  needle[i]:k  1next.append(k)j  0for i in range(n):while j  0 and haystack[i] ! needle[j]:j  next[j-1]if haystack[i]  needle[j]:j  1if j  m:return i - j  1return -1  
提示 1  haystack.length, needle.length  104 haystack 和 needle 仅由小写英文字符组成 
来源力扣LeetCode 链接https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 
求解一个给定的方程将x以字符串 “x#value” 的形式返回。该方程仅包含 ‘’  ‘-’ 操作变量 x 和其对应系数。 如果方程没有解或存在的解不为整数请返回 “No solution” 。如果方程有无限解则返回 “Infinite solutions” 。 题目保证如果方程中只有一个解则 ‘x’ 的值是一个整数。 示例1 
输入: equation  x5-3x6x-2
输出: x2示例2 
输入: equation  xx
输出: Infinite solutions示例3 
输入: equation  2xx
输出: x0提示: 3  equation.length  1000 equation 只有一个 ‘’. 方程由绝对值在 [0, 100] 范围内且无任何前导零的整数和变量 ‘x’ 组成。 题解 
class Solution:def scanner(self, strs):x, nums  0, 0len_strs  len(strs)if strs[0]  -1:sign  -1else:sign  1num  0flag  -1for start in range(len_strs):if strs[start]  -:nums  sign * numnum, mul  0, 0sign  -1elif strs[start]  :nums  sign * numnum, mul  0, 0sign  1elif strs[start]  x:if num  0:if flag  0:x  0else:x  1 * signelse:x  sign * numnum, mul  0, 0else:num  10 * num  int(strs[start])if num  0:flag  0if num ! 0:nums  sign * numreturn x, numsdef solveEquation(self, equation: str) - str:left, right  equation.split()left_x, left_nums  self.scanner(left)right_x, right_nums  self.scanner(right)x  left_x - right_x nums  right_nums - left_numsif x  0:if nums  0:return Infinite solutionselse:return No solutionif x ! 0 and nums  0:return x0return fx{nums//x}来源力扣LeetCode 链接https://leetcode.cn/problems/solve-the-equation 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 文章转载自: http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.vtbtje.cn.gov.cn.vtbtje.cn http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.qpqb.cn.gov.cn.qpqb.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.pkdng.cn.gov.cn.pkdng.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.zyffq.cn.gov.cn.zyffq.cn http://www.morning.kmqwp.cn.gov.cn.kmqwp.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.kpcjl.cn.gov.cn.kpcjl.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn