
算法练手
vancooler
cs学徒/天天偷看大牛的博客/SDU
展开
-
记录一下华为的两次机试:找出有向图中所有的圈并且输出/算合法字符串/计算器
华为的一道机试题目,不知道为什么自己脑子抽了一下想了很久很久(其实主要还是DFS太不熟了代码也不是很elegant,将就记录一下。class Node: def __init__(self, data, next = None): self.data = data self.next = nextdef dfs(graph, stack, des...原创 2020-03-11 18:47:04 · 404 阅读 · 0 评论 -
9.10 练手 88
Leetcode 88.Merge Sorted ArrayGiven two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandn...原创 2019-09-10 23:16:20 · 107 阅读 · 0 评论 -
9.9 练手 腾讯50题 62
Leetcode62.Unique PathsA robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The rob...原创 2019-09-09 09:46:57 · 105 阅读 · 0 评论 -
9.8 练手 腾讯50题 59螺旋矩阵2
Leetcode 59.Spiral Matrix IIGiven a positive integern, generate a square matrix filled with elements from 1 ton2in spiral order.Example:Input: 3Output:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7,...原创 2019-09-08 18:18:38 · 91 阅读 · 0 评论 -
9.7 练手 腾讯50题 54螺旋矩阵
Leecode54.Spiral MatrixGiven a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]O...原创 2019-09-07 20:09:19 · 113 阅读 · 0 评论 -
9.5 练手 腾讯50题 Search in Rotated Sorted Array
Leetcode33.Search in Rotated Sorted ArraySuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e.,[0,1,2,4,5,6,7]might become[4,5,6,7,0,1,2]).You...原创 2019-09-05 18:00:34 · 93 阅读 · 0 评论 -
9.4 练手 腾讯50题 3Sum
Leetcode15.3SumGiven an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:The solutio...原创 2019-09-04 18:08:49 · 83 阅读 · 0 评论 -
9.3练手 腾讯50题 16最接近三数和
Leetcode16.3Sum ClosestGiven an arraynumsofnintegers and an integertarget, find three integers innumssuch that the sum is closest totarget. Return the sum of the three integers. You may a...原创 2019-09-03 20:25:06 · 111 阅读 · 0 评论 -
9.2 练手 腾讯50题 11算水槽容量
11.Container With Most WaterGivennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis...原创 2019-09-02 18:44:03 · 227 阅读 · 0 评论 -
8.31 练手 腾讯50题 643
Leetcode643.Maximum Average Subarray IGiven an array consisting ofnintegers, find the contiguous subarray of given lengthkthat has the maximum average value. And you need to output the maximum ...原创 2019-08-31 22:43:33 · 84 阅读 · 0 评论 -
8.30 练手 腾讯50题 83
Leetcode83.Remove Duplicates from Sorted ListGiven a sorted linked list, delete all duplicates such that each element appear onlyonce.Example 1:Input: 1->1->2Output: 1->2Exampl...原创 2019-08-30 19:18:05 · 87 阅读 · 0 评论 -
9.11 练手 腾讯50题 121
Leetcode121.Best Time to Buy and Sell StockSay you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction ...原创 2019-09-12 08:39:41 · 175 阅读 · 0 评论 -
9.12练手 腾讯50题 217
Leetcode217.Contains DuplicateGiven an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it sho...原创 2019-09-12 08:53:12 · 87 阅读 · 0 评论 -
9.29 练手 腾讯50题 344.颠倒字符串
Leetcode344.Reverse StringWrite a function that reverses a string. The input string is given as an array of characterschar[].Do not allocate extra space for another array, you must do this by...原创 2019-09-29 22:52:27 · 153 阅读 · 0 评论 -
9.28 练手 腾讯50题 9.回文数 43.字符串数字相乘
Leetcode 9.Palindrome Number思路是先转换成字符串然后两边双指针检测,代码如下:class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ if x<0: ...原创 2019-09-28 20:24:13 · 134 阅读 · 0 评论 -
9.26 练手 腾讯50题 8.atoi
Leetcode 8.String to Integer (atoi)如何将字符串转为有效数字题,代码如下:class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ if str == None...原创 2019-09-26 22:08:39 · 149 阅读 · 0 评论 -
9.23 练手 腾讯50题 237 删除链表
Leetcode237.Delete Node in a Linked List给一个节点,在链表中删除这个节点,不返回任何值。代码如下:class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do n...原创 2019-09-23 21:59:20 · 109 阅读 · 0 评论 -
9.22 练手 腾讯50题 160 寻找两链表交点
Leetcode160.Intersection of Two Linked ListsWrite a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to...原创 2019-09-22 22:09:01 · 114 阅读 · 0 评论 -
9.21 练手 腾讯50题 反转链表
Leetcode206.Reverse Linked ListReverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be re...原创 2019-09-21 23:34:40 · 108 阅读 · 0 评论 -
9.19 练手 腾讯50题 142 链表找回环II
Leetcode142.Linked List Cycle II步骤为:判断是否有环 —>没有 return None —>有 一个一个走直到在环的入口相遇代码如下:class Solution(object): def detectCycle(self, head): """ ...原创 2019-09-19 18:42:42 · 95 阅读 · 0 评论 -
9.17 练手 腾讯50题 61旋转链表
Leetcode61.Rotate ListGiven a linkedlist, rotate the list to the right bykplaces, wherekis non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1-...原创 2019-09-17 00:29:41 · 110 阅读 · 0 评论 -
9.15 练手 腾讯50题 2两数相加
Leetcode2.Add Two NumbersYou are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain a single digit. Ad...原创 2019-09-15 12:11:51 · 116 阅读 · 0 评论 -
9.13练手 腾讯50题 238 除了自己之外的乘积
Leetcode238.Product of Array Except SelfGiven an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnu...原创 2019-09-13 22:21:10 · 99 阅读 · 0 评论 -
8.29 练手 腾讯50题 4
Leetcode4.Median of Two Sorted ArraysThere are two sorted arraysnums1andnums2of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be...原创 2019-08-29 18:54:56 · 113 阅读 · 0 评论 -
8.16 练手 腾讯50题 104
Leetcode104.Maximum Depth of Binary Tree寻找二叉树的最大深度Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the fa...原创 2019-08-16 20:12:54 · 82 阅读 · 0 评论 -
8.13练手 腾讯50题 169
Leetcode 169.Majority ElementGiven an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.You may assume that the array is non-em...原创 2019-08-12 20:43:41 · 102 阅读 · 0 评论 -
4.8-4.9 练手
Leetcode 150.Evaluate Reverse Polish Notation第一次提交: faster than 26%+ 暴风哭泣import mathclass Solution(object): def opProcess(self, op, num1 , num2): if op == 0: return num1...原创 2019-04-08 15:53:25 · 130 阅读 · 0 评论 -
4.2 练手
Leetcode 7Reverse IntegerGiven a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output:...原创 2019-04-02 18:07:02 · 125 阅读 · 0 评论 -
4.11 練手
考虑先从自己之后学的树等算法刷起, leetcode, 我不想看论文时的娱乐活动Leetcode 100 Same Tree递归前序遍历,但是不知道为什么每次submit的得到的时间不太一样,玄学地很,代码如下:class Solution(object): def isSameTree(self, p, q): """ :type p: Tr...原创 2019-04-11 16:16:08 · 84 阅读 · 0 评论 -
4.7 練手
Leetcode21.Merge Two Sorted Lists一开始解决方式(特别慢,特别乱: beat 10%+ 我个菜class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode...原创 2019-04-07 16:02:56 · 99 阅读 · 0 评论 -
4.1 練手
Leetcode 1 Two SumGiven an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would haveexactlyone solution, and you m...原创 2019-04-01 22:01:35 · 99 阅读 · 0 评论 -
4.6 练手
Leetcode 12.Integer to RomanRoman numerals are represented by seven different symbols:I,V,X,L,C,DandM.Symbol ValueI 1V 5X 10L 5...原创 2019-04-06 12:03:07 · 195 阅读 · 0 评论 -
4.5 练手
Leetcode 14Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string"".Example 1:Input...原创 2019-04-05 21:46:57 · 114 阅读 · 0 评论 -
4.4 练手
leetcode13.Roman to IntegerRoman numerals are represented by seven different symbols:I,V,X,L,C,DandM.Symbol ValueI 1V 5X 10L 5...原创 2019-04-04 21:55:48 · 137 阅读 · 0 评论 -
3.31 练手
Leetcode 23题Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[ 1->4->5, 1->3->4, 2->6]Output: 1->1->...原创 2019-03-31 12:11:23 · 125 阅读 · 0 评论 -
练手 3.30
转码艺术生,小白刷题练手,有些是讨论里面觉得解法不错的,轻喷。剑指Offer输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。递归方法:class Solution: def TreeDepth(self, pRoot): # write code here i...原创 2019-03-30 22:16:17 · 124 阅读 · 0 评论 -
4.9 练手
Leetcode141.Linked List Cycle简单的链表题,我的代码:class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ while head: ...原创 2019-04-09 22:17:57 · 169 阅读 · 0 评论 -
8.4练手 腾讯50题
leetcode155 实现最小栈设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。push(x)-- 将元素 x 推入栈中。pop()-- 删除栈顶的元素。top()-- 获取栈顶元素。getMin() -- 检索栈中的最小元素。示例:MinStack minStack = new MinStack();minStack.push(-...原创 2019-08-04 17:29:31 · 240 阅读 · 0 评论 -
8.12 练手 腾讯50题 136
Leetcode136.Single NumberGiven anon-emptyarray of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Cou...原创 2019-08-12 18:13:45 · 92 阅读 · 0 评论 -
8.26 练手 腾讯50题 146 292
Leetcode146.LRU Cache运用你所掌握的数据结构,设计和实现一个LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据get和 写入数据put。获取数据get(key)- 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。写入数据put(key, value)- 如果密钥不存在,则写入其数据值。当缓存容量...原创 2019-08-26 01:10:29 · 304 阅读 · 1 评论