Responsive image

度孝子别急出来干你们 - 2021-01-20 16:43 [GF]
这力扣的题怎么就做不明白呢
我以前考研刷什么张宇李永乐汤家凤也没觉得这么难啊
怎么到了力扣连答案都看不明白


accelerate12138 - 2021-01-20 16:47 [B1F]
leetcode不是用c或者c艹来刷的吗
python也能刷?


地瓜叶 - 2021-01-20 16:48 [B2F]
东西坏了就拍一拍,先给自己来两嘴巴子试试


mintQ - 2021-01-20 16:49 [B3F]
算法要专门学的。我当年打竞赛的,这2本入门不错:挑战程序设计竞赛 2 和 挑战程序设计竞赛 1


据说一般人看这个也不错:程序员代码面试指南 IT名企算法与数据结构题目最优解


想深入点学可以看看算法概论。算法导论想必你也看不来。

话说回来,python不适合刷算法题。学下c++的基础吧,3天入门不难


哈尔滨佛学院 - 2021-01-20 16:55 [B4F]
Leetcode 不是有手就行?


   - 2021-01-20 17:00 [B5F]
有没有其他语言的编程基础?

没有的话,学了几天python就去刷Leetcode??大神???


我不是colaice - 2021-01-20 17:03 [B6F]
python写算法有点抽象,只要你有思路多写写就好了。发个我写过的二叉查找树代码参考下。
class BSTNode(object):
    def __init__(self, key, value, left=None, right=None):
        self.key,self.value,self.left,self.right = key,value,left,right



class BST(object):
    def __init__(self,root=None):
        self.root = root

    @classmethod
    def build_from(cls,node_list):
        cls.size = 0
        key_to_node_dict = {}
        for node_dict in node_list:
            key = node_dict['key']
            key_to_node_dict[key] = BSTNode(key, value=key)
        
        for node_dict in node_list:
            key = node_dict['key']
            node = key_to_node_dict[key]
            if node_dict['is_root']:
                root = node
            node.left = key_to_node_dict.get(node_dict['left'])
            node.right = key_to_node_dict.get(node_dict['right'])
        return cls(root)
    
    def _bst_search(self, subtree, key):
        if subtree is None:
            return None
        elif key < subtree.key:
            return self._bst_search(subtree.left, key)
        elif key > subtree.key:
            return self._bst_search(subtree.right, key)
        else:
            return subtree
    
    def get(self, key, default=None):
        node = self._bst_search(self.root, key)
        if node is None:
            return default
        else:
            return node.value
    
    def __cantains__(self,key):
        return self._bst_search(self.root, key) is not None

    def _bst_min_node(self, subtree):
        if subtree is None:
            return None
        elif subtree.left is None:
            return subtree
        else:
            return self._bst_min_node(subtree.left)
    
    def _bst_max_node(self, subtree):
        if subtree is None:
            return None
        elif subtree.right is None:
            return subtree
        else:
            return self._bst_max_node(subtree.right)

    def bst_min(self):
        node = self._bst_min_node(self.root)
        return node.value if node else None
    
    def bst_max(self):
        node = self._bst_max_node(self.root)
        return node.value if node else None
    
    def _bst_insert(self, subtree, key, value):
        if subtree is None:
            subtree = BSTNode(key, value)
        elif key < subtree.key:
            subtree.left = self._bst_insert(subtree.left, key, value)
        elif key > subtree.key:
            subtree.right = self._bst_insert(subtree.right, key, value)
        return subtree
    
    def add(self, key, value):
        node = self._bst_search(self.root, key)
        if node is not None:
            node.value = value
            return False
        else:
            self.root = self._bst_insert(self.root, key, value)
            self.size += 1
            return True
        
    def _bst_remove(self, subtree, key):
        if subtree is None:
            return None
        elif key < subtree.key:
            subtree.left = self._bst_remove(subtree.left, key)
            return subtree
        elif key > subtree.key:
            subtree.right = self._bst_remove(subtree.right, key)
            return subtree
        else:                                                            #如果找到了要删除的节点
            if subtree.left is None and subtree.right is None:            #是叶子
                return None
            elif subtree.left is None or subtree.right is None:            #单孩子
                if subtree.left is not None:
                    return subtree.left                                    #返回孩子并更换父节点指向    
                else:
                    return subtree.right
            else:                                                        #双孩子情况,寻找逻辑后继并且替换被删除节点
                successor_node = self._bst_min_node(subtree.right)
                subtree.key, subtree.value = successor_node.key, successor_node.value
                subtree.right = self._bst_remove(subtree.right, subtree.key)
                return subtree
    def remove(self, key):
        self.size -= 1
        return self._bst_remove(self.root, key)

NODE_LIST = [
    {'key':60, 'left':12, 'right':90, 'is_root':True},
    {'key':12, 'left':4, 'right':41, 'is_root':False},
    {'key':4, 'left':1, 'right':None, 'is_root':False},
    {'key':1, 'left':None, 'right':None, 'is_root':False},
    {'key':41, 'left':29, 'right':None, 'is_root':False},
    {'key':29, 'left':23, 'right':37, 'is_root':False},
    {'key':23, 'left':None, 'right':None, 'is_root':False},
    {'key':37, 'left':None, 'right':None, 'is_root':False},
    {'key':90, 'left':71, 'right':100, 'is_root':False},
    {'key':71, 'left':None, 'right':84, 'is_root':False},
    {'key':100, 'left':None, 'right':None, 'is_root':False},
    {'key':84, 'left':None, 'right':None, 'is_root':False},
]

def test_bst_tree():
    bst = BST.build_from(NODE_LIST)
    for node_dict in NODE_LIST:
        key = node_dict['key']
        assert bst.get(key) == key
    assert bst.get(-1) is None
    assert bst.bst_min() == 1
    bst.add(0,0)
    assert bst.bst_min() == 0
    bst.remove(12)
    assert bst.get(12) is None
    bst.remove(1)
    assert bst.get(1) is None


还有简单的体现,比如计数排序,好像叫技术排序还是桶排序来着。。
def bucket_sort(array):

    maxnum = max(array)

    bucket = [0] * (maxnum + 1)

    for i in array:
        bucket += 1
    
    newarray = []

    for j in range(len(bucket)):
        if bucket[j] != 0:
            for _ in range(bucket[j]):
                newarray.append(j)
    return newarray

array = [5,6,3,2,1,65,2,0,8,0]
print(bucket_sort(array))


度孝子别急出来干你们 - 2021-01-20 17:05 [B7F]
   好兄弟


crossgame - 2021-01-20 17:23 [B8F]
要刷leetcode要专门学习数据结构和算法的,如果不想上课看书就去网上搜搜题解,能教你如何理解和运用


非吾愆期 - 2021-01-20 17:26 [B9F]
刷力扣要回很多数据结构的,其实都是特定题型的变种罢了,如果只是单纯的暴力解其实是没有打到出题者的目的的,力扣上有各种数据结构的题型自己多去看。


ShockmaN - 2021-01-20 20:04 [B10F]
去学数据结构和算法。不过如果你是刚开始接触编程的话,建议你还是老老实实把python基础知识学完再说。


99a404d3 - 2021-01-21 12:05 [B11F]
看不懂很正常。初学三年之后能看懂就不错了。先慢慢走,跑都不会,就别想着飞了。


fdf02516 - 2021-01-21 12:28 [B12F]
好几天是几天啊,代码这东西不自己从头写,光看或是照书抄是学不会的


HTNMK - 2021-01-21 12:30 [B13F]
算法是算法,语言是语言,两个不一样的


233 - 2021-01-21 13:29 [B14F]
建议再多学几天。。。


quwindxx - 2021-01-21 21:19 [B15F]
学到知识了!1


女王的枕头 - 2021-01-21 21:33 [B16F]
算法要先学数据结构而不是语言

了解基础的hash,链表双链表,树,栈和队列及其应用,然后就可以开始拓展了


好想要六花啊 - 2021-01-21 21:36 [B17F]
学几天怎么能刷算法题


墨心 - 2021-01-21 21:40 [B18F]
力扣的题都是算法了,刚学几天可能你能写个像样的小程序,但直接刷算法题肯定懵逼。


111000010 - 2021-01-21 22:37 [B19F]
诶 我反而觉得学几天语言就能刷算法题诶,基础的语法和数据结构学明白后,刷算法就和语言无关了。可以先从简单的题刷起,比如大一的课后作业其实也能算作算法题(打印什么杨辉三角啊、罗汉塔啊)


b00dfb84 - 2021-01-22 13:19 [B20F]
静下心来就可以了,你可能其他的事情太多了






桌面版


Powered by SP Project v1.0 © 2010-2019
Time 0.002438 second(s),query:3 Gzip enabled


Top