Course Schedule 安排课表   Frog Jump  最长回文字符串长度

lincode 题目记录5,lincode记录5

Course Schedule 

选课方案问题,题目说的很清楚了就是bfs或者dfs,然后加个字典优化,弄了好久没弄出来··网上查的算法··提交的是bfs的算法,dfs的算法给的用例好像是递归太深了·
python编译器直接崩了··本地调试也是直接崩了··

bfs的核心是用一个数组记录每个数字的深度··就是要上这个课之前你得上多少门课,把是0的都加到队列里边,后边遍历的时候遍历到这个点的时候
减一,直到减完,遍历完了如果还有点的入度不为0 ,就不能完成

网站上的python不知道是啥版本·· 队列是大写的Q ,我本地的是python3.5
是小写的 ··dfs的递归好像通不过

class Solution:
    # @param {int} numCourses a total of n courses
    # @param {int[][]} prerequisites a list of prerequisite pairs
    # @return {boolean} true if can finish all courses or false
    def canFinish(self, numCourses, prerequisites):
        import Queue
        import sys
        sys.setrecursionlimit(1000000)
        if len(prerequisites)<2:return True
        dicp={}
        inlist=[0]*numCourses
        for p in prerequisites:
            dicp.setdefault(p[1],[])
            dicp[p[1]].append(p[0])
            inlist[p[0]] += 1
        q=Queue.Queue()
        for i in range(numCourses):
            if inlist[i] == 0:q.put(i)

        while not q.empty():
            cur=q.get()
            if cur not in dicp:continue
            for i in dicp[cur]:
                inlist[i] -=1
                if inlist[i]==0:q.put(i)

        for i in inlist:
            if i !=0 :return False

        return True  
        ''' DFS  
        visit=[0]*numCourses
        def canfinishdfs(visit,i):
            if visit[i]==-1:return False
            if visit[i]==1:return True
            visit[i]=-1
            if i in dicp:
                for cur in dicp[i]:
                    if not canfinishdfs(visit,cur):return False
            visit[i]=1
            return True

        for i in range(numCourses):
             if not canfinishdfs(visit,i):
                 return False

        return True
        '''

 安排课程问题,跟上边一样··
加一个数组输出就行了·最后可以选课就是输出,不能就是输出空数组

class Solution:
    # @param {int} numCourses a total of n courses
    # @param {int[][]} prerequisites a list of prerequisite pairs
    # @return {int[]} the course order
    def findOrder(self, numCourses, prerequisites):
        # Write your code here
        import Queue
        dicp={}
        inlist=[0]*numCourses
        for p in prerequisites:
            dicp.setdefault(p[1],[])
            dicp[p[1]].append(p[0])
            inlist[p[0]] += 1
        q=Queue.Queue()
        res=[]
        for i in range(numCourses):
            if inlist[i] == 0:
                q.put(i)
                res.append(i)

        while not q.empty():
            cur=q.get()
            if cur not in dicp:continue
            for i in dicp[cur]:
                inlist[i] -=1
                if inlist[i]==0:
                    q.put(i)
                    res.append(i)

        for i in inlist:
            if i !=0 :
                return []
        return res

Frog Jump

跳青蛙问题,从后往前遍历,从倒数第3个开始计算是否能到达该点,DFS算法,第一个递归的一个循环的··,关键在那个失败的缓存节点,

if len(stones)==1:return True
        # memo=set()
        # target=stones[-1]
        # stones=set(stones)

        # def bt(cur,k):
        #     if (cur,k) in memo:
        #         return False
        #     if cur==target:
        #         return True
        #     if cur>target or cur<0 or k<=0 or cur not in stones:
        #         return False
        #     dirs=[k-1,k,k+1]
        #     for c in dirs:
        #         if (cur+c)in stones:
        #             if bt(cur+c,c):
        #                 return True
        #     memo.add((cur,k))
        #     return False
        # return bt(0,0)

        stone_set, fail = set(stones), set()
        stack = [(0, 0)]
        while stack:
            stone, jump = stack.pop()
            for j in (jump-1, jump, jump+1):
                s = stone + j
                if j > 0 and s in stone_set and (s, j) not in fail:
                    if s == stones[-1]:
                        return True
                    stack.append((s, j))
            fail.add((stone, jump))
        return False

 最长回文字符串长度·直接分组遍历就行了:

class Solution:
    # @param {string} s a string which consists of lowercase or uppercase letters
    # @return {int} the length of the longest palindromes that can be built
    def longestPalindrome(self, s):
        # Write your code here
        dics={}
        for i in s:
            dics.setdefault(i,[])
            dics[i].append(i)
        sum=0
        flag=False
        for di in dics:
            n=len(dics[di])
            if n>1:
                if n%2 !=0:
                    sum+=n-1
                    flag=True
                else:
                    sum+=n
            else:
                flag=True
        if flag:sum+=1
        return sum

 

Course Schedule 安排课表   Frog Jump  最长回文字符串长度

Course Schedule 

选课方案问题,题目说的很清楚了就是bfs或者dfs,然后加个字典优化,弄了好久没弄出来··网上查的算法··提交的是bfs的算法,dfs的算法给的用例好像是递归太深了·
python编译器直接崩了··本地调试也是直接崩了··

bfs的核心是用一个数组记录每个数字的深度··就是要上这个课之前你得上多少门课,把是0的都加到队列里边,后边遍历的时候遍历到这个点的时候
减一,直到减完,遍历完了如果还有点的入度不为0 ,就不能完成

网站上的python不知道是啥版本·· 队列是大写的Q ,我本地的是python3.5
是小写的 ··dfs的递归好像通不过

class Solution:
    # @param {int} numCourses a total of n courses
    # @param {int[][]} prerequisites a list of prerequisite pairs
    # @return {boolean} true if can finish all courses or false
    def canFinish(self, numCourses, prerequisites):
        import Queue
        import sys
        sys.setrecursionlimit(1000000)
        if len(prerequisites)<2:return True
        dicp={}
        inlist=[0]*numCourses
        for p in prerequisites:
            dicp.setdefault(p[1],[])
            dicp[p[1]].append(p[0])
            inlist[p[0]] += 1
        q=Queue.Queue()
        for i in range(numCourses):
            if inlist[i] == 0:q.put(i)

        while not q.empty():
            cur=q.get()
            if cur not in dicp:continue
            for i in dicp[cur]:
                inlist[i] -=1
                if inlist[i]==0:q.put(i)

        for i in inlist:
            if i !=0 :return False

        return True  
        ''' DFS  
        visit=[0]*numCourses
        def canfinishdfs(visit,i):
            if visit[i]==-1:return False
            if visit[i]==1:return True
            visit[i]=-1
            if i in dicp:
                for cur in dicp[i]:
                    if not canfinishdfs(visit,cur):return False
            visit[i]=1
            return True

        for i in range(numCourses):
             if not canfinishdfs(visit,i):
                 return False

        return True
        '''

 安排课程问题,跟上边一样··
加一个数组输出就行了·最后可以选课就是输出,不能就是输出空数组

class Solution:
    # @param {int} numCourses a total of n courses
    # @param {int[][]} prerequisites a list of prerequisite pairs
    # @return {int[]} the course order
    def findOrder(self, numCourses, prerequisites):
        # Write your code here
        import Queue
        dicp={}
        inlist=[0]*numCourses
        for p in prerequisites:
            dicp.setdefault(p[1],[])
            dicp[p[1]].append(p[0])
            inlist[p[0]] += 1
        q=Queue.Queue()
        res=[]
        for i in range(numCourses):
            if inlist[i] == 0:
                q.put(i)
                res.append(i)

        while not q.empty():
            cur=q.get()
            if cur not in dicp:continue
            for i in dicp[cur]:
                inlist[i] -=1
                if inlist[i]==0:
                    q.put(i)
                    res.append(i)

        for i in inlist:
            if i !=0 :
                return []
        return res

Frog Jump

跳青蛙问题,从后往前遍历,从倒数第3个开始计算是否能到达该点,DFS算法,第一个递归的一个循环的··,关键在那个失败的缓存节点,

if len(stones)==1:return True
        # memo=set()
        # target=stones[-1]
        # stones=set(stones)

        # def bt(cur,k):
        #     if (cur,k) in memo:
        #         return False
        #     if cur==target:
        #         return True
        #     if cur>target or cur<0 or k<=0 or cur not in stones:
        #         return False
        #     dirs=[k-1,k,k+1]
        #     for c in dirs:
        #         if (cur+c)in stones:
        #             if bt(cur+c,c):
        #                 return True
        #     memo.add((cur,k))
        #     return False
        # return bt(0,0)

        stone_set, fail = set(stones), set()
        stack = [(0, 0)]
        while stack:
            stone, jump = stack.pop()
            for j in (jump-1, jump, jump+1):
                s = stone + j
                if j > 0 and s in stone_set and (s, j) not in fail:
                    if s == stones[-1]:
                        return True
                    stack.append((s, j))
            fail.add((stone, jump))
        return False

 最长回文字符串长度·直接分组遍历就行了:

class Solution:
    # @param {string} s a string which consists of lowercase or uppercase letters
    # @return {int} the length of the longest palindromes that can be built
    def longestPalindrome(self, s):
        # Write your code here
        dics={}
        for i in s:
            dics.setdefault(i,[])
            dics[i].append(i)
        sum=0
        flag=False
        for di in dics:
            n=len(dics[di])
            if n>1:
                if n%2 !=0:
                    sum+=n-1
                    flag=True
                else:
                    sum+=n
            else:
                flag=True
        if flag:sum+=1
        return sum

 

http://www.bkjia.com/Pythonjc/1216744.htmlwww.bkjia.comtruehttp://www.bkjia.com/Pythonjc/1216744.htmlTechArticlelincode 题目记录5,lincode记录5 Course Schedule
安排课表 Frog Jump 最长回文字符串长度 Course Schedule
选课方案问题,题目说的很清楚了就是bfs或者…

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注