leetcode
笑个不停
欢迎machine learning届各位大佬多多指教!
展开
-
Linked List Cycle(判断链表是否有环)-python
Given a linked list, determine if it has a cycle in it.解答:定义两个指针分别从链表头部开始,一个指针每次走一步,另一个指针每次走两步,若两个指针所指的值相等,则说明有环,否则没环;要注意指针所指之处是否为空class Solution(object): def hasCycle(self, head): """ ...原创 2018-03-22 12:55:43 · 871 阅读 · 0 评论 -
leetcode-easy-python
1、Excel Sheet Column TitleGiven a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 2...原创 2018-06-24 12:25:40 · 275 阅读 · 0 评论 -
leetcode-sql
1、Second Highest SalaryWrite a SQL query to get the second highest salary from the Employee table.+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+...原创 2018-06-24 11:58:26 · 222 阅读 · 0 评论 -
top interviews questions-leetcode-python附代码详解
1、Convert Sorted Array to Binary Search Tree(将升序的数组转换成平衡二叉树):2、Happy Number3、Min Stack-实现返回最小元素的栈4、Plus One数组的值转成数值,加1之后,输出值的列表形式5、判断一个数是否是3的幂6、 Pascal's Triangle打印出帕斯卡三角形7、Pascal's Triangle II得到帕斯卡三角...原创 2018-04-17 23:55:03 · 674 阅读 · 0 评论 -
Remove Duplicates from Sorted List-python
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.解答:需要注意链...原创 2018-03-22 12:35:34 · 130 阅读 · 0 评论 -
Merge Two Sorted Lists-python
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->1...原创 2018-03-22 11:04:14 · 446 阅读 · 0 评论 -
Palindrome Number(判断数字是不是回文数)-python
Determine whether an integer is a palindrome. Do this without extra space.解答 : 有的答案是把数转成字符串,逆序之后与原来的相等,那么就是回文数;本人提供的答案是,设置两个指针,分别指向头部和尾部,对比对应的数是否相等,直到指针指向中间位置,结束。class Solution(object): def isPalin...原创 2018-03-22 10:54:57 · 482 阅读 · 0 评论 -
Reverse Integer-python
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an envi...原创 2018-03-22 10:38:28 · 421 阅读 · 0 评论 -
leetcode-Two sum(最佳思路以及python代码实现)
1、Two sumGiven nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].答案:使用hashtable,建立数组值和下标的键值对,在python中相当于使用dict来存储,dict的key是数组的值,数组的下标是dict的value。class Solution(o...原创 2018-03-22 10:17:57 · 2309 阅读 · 2 评论 -
Array-leetcode-python
1、Array Partition IGiven an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to...原创 2018-03-26 14:13:43 · 234 阅读 · 0 评论 -
leetcode-python-binary search
1、Two Sum II - Input array is sortedGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should ...原创 2018-03-25 19:50:43 · 250 阅读 · 0 评论 -
leetcode-Tree-python
1、Same TreeGiven two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.E...原创 2018-03-23 16:11:08 · 218 阅读 · 0 评论 -
Minimum Index Sum of Two Lists-python
解答:运用python的dict快速创建hash索引class Solution(object): def findRestaurant(self, list1, list2): pos1={v:pos for pos,v in enumerate(list1)} # 对list构建字典索引,键是数组的值,值是数组的下标 length=len(list2)+len...原创 2018-03-22 14:03:20 · 207 阅读 · 0 评论 -
Intersection of Two Linked Lists-python
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘ c...原创 2018-03-22 13:36:29 · 196 阅读 · 0 评论 -
Linked List Cycle II-python
解答:要想得知链表环开始的地方,需要先判断链表是否有环(参考本博客Linked List Cycle),当判断为有环时,记住当前所指的位置,将其中一个指针重置到链表头部,两个指针开始以相同步长1往后遍历链表,当再次相遇时,即环开始的地方class Solution: # @param head, a ListNode # @return a list node def detec...原创 2018-03-22 13:08:36 · 224 阅读 · 0 评论 -
leetcode-top100-liked-questions-medium
1、Target SumYou are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.Find...原创 2018-07-11 20:39:38 · 624 阅读 · 0 评论