![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 52
Rachel-chen
不积跬步无以至千里
展开
-
169. Majority Element (python)
Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majo原创 2016-10-30 16:08:15 · 649 阅读 · 0 评论 -
189. Rotate Array (python)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can,原创 2016-10-30 16:09:17 · 632 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock (python)
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), d原创 2016-10-30 16:06:30 · 377 阅读 · 0 评论 -
119. Pascal's Triangle II (python)
Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题意:返回杨辉三角的k行 思路:和1原创 2016-10-30 16:05:46 · 356 阅读 · 0 评论 -
118. Pascal's Triangle (python)
Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意:每一层的第i个位置,等于上一层第i-1与第i原创 2016-10-30 16:04:38 · 1860 阅读 · 0 评论 -
88. Merge Sorted Array(python)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addition原创 2016-10-30 16:03:19 · 354 阅读 · 0 评论 -
66. Plus One (python)
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题意:给一个非负数,用数组表示,加1返回结果 思原创 2016-10-30 16:01:45 · 409 阅读 · 0 评论 -
27. Remove Element (python)
Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The o原创 2016-10-30 16:00:56 · 779 阅读 · 0 评论 -
1. Two Sum (python)
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7,原创 2016-10-30 15:57:27 · 1989 阅读 · 0 评论 -
217. Contains Duplicate (python)
Given 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 should return false if every element原创 2016-10-30 16:10:26 · 401 阅读 · 0 评论 -
219. Contains Duplicate II (python)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 题意:判断是原创 2016-10-30 16:11:05 · 646 阅读 · 0 评论 -
31. Next Permutation (python)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible or原创 2016-10-30 16:17:51 · 2130 阅读 · 1 评论 -
18. 4Sum (python)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution se原创 2016-10-30 16:16:56 · 1039 阅读 · 0 评论 -
16. 3Sum Closest (python)
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2016-10-30 16:16:08 · 495 阅读 · 0 评论 -
15. 3Sum (python)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain dup原创 2016-10-30 16:15:26 · 547 阅读 · 0 评论 -
Ksum问题 (leetcode)
1,15,16,18 转载自:http://www.sigmainfy.com/blog/summary-of-ksum-problems.html 方法一:暴力搜索法 时间复杂度O(n^k) 方法二:排序 2sum:采用快速排序法,排序后采用前后指针。当和为target,则找到答案返回;当和大于target,时,右指针左移;当和小于target时,左指针右移。循环上述过程,若找到则返回,否原创 2016-10-30 16:14:36 · 477 阅读 · 0 评论 -
11. Container With Most Water (python)
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2016-10-30 16:13:40 · 360 阅读 · 0 评论 -
396. Rotate Function (python)
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow: F(k) = 0 *原创 2016-10-30 16:12:42 · 1270 阅读 · 0 评论 -
283. Move Zeroes (python)
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your fun原创 2016-10-30 16:11:38 · 390 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array (python)
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with co原创 2016-10-30 15:58:23 · 2738 阅读 · 0 评论 -
328. Odd Even Linked List (python)
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in pl原创 2016-09-19 22:13:42 · 375 阅读 · 0 评论 -
148. Sort List (python)
Sort a linked list in O(n log n) time using constant space complexity. 思路:能够有O(n lgn)时间复杂度的算法为,快速排序,堆排序,归并排序,三者的空间复杂度分别为O(1), O(N),O(N) 其中归并排序,其的基本思路就是将数组分成二组A,B,如果这二组组内的数据都是有序的,那么就可以很方便的将这二组数据进行排序。如原创 2016-09-19 22:12:26 · 942 阅读 · 0 评论 -
237. Delete Node in a Linked List(python)
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3,原创 2016-09-18 21:47:47 · 673 阅读 · 0 评论 -
2、add two numbers(python)
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linke原创 2016-09-18 21:49:47 · 1775 阅读 · 0 评论 -
61. Rotate List(python)
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 题意:链表向右移动K位 Runtime: 42 ms 思路: 获取链表原创 2016-09-18 21:50:31 · 332 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II(python)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1-原创 2016-09-18 21:52:00 · 298 阅读 · 0 评论 -
86. Partition List(python)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of th原创 2016-09-18 21:53:13 · 436 阅读 · 0 评论 -
92. Reverse Linked List II(python)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follow原创 2016-09-18 21:54:15 · 344 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree(python)
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:将有序单链表转换成二叉搜索树 思路:采用递归的思想,寻找中点构造二叉搜索树,左子树和右子树分别递归即可,注意递归停止的条件:无结点或者只有一个叶子结点 注意:无结点时返回原创 2016-09-18 21:55:07 · 462 阅读 · 0 评论 -
142. Linked List Cycle II(python)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? Subscribe原创 2016-09-18 21:56:31 · 436 阅读 · 0 评论 -
234. Palindrome Linked List(python)
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 题意:给定一个链表,判断其是否为回文链表 eg:1 2 3 2 1 or 1 2 2 1 思路:找到中心点,前后对比思路一:考虑到单链表缺失从后向前的信息(原创 2016-09-18 21:45:15 · 1021 阅读 · 0 评论 -
206. Reverse Linked List(python)
题意:翻转链表思路一:入栈出栈,利用数组模拟栈,入栈即每次在数组的开头加入数,出栈即从数组的开头开始 空间复杂度为O(n) Runtime: 76 ms思路二:头结点倒插,类似于思路一,原链表头结点依次插入新链表的开头,返回新链表 空间复杂度为O(1) Runtime: 68 ms思路三:递归!!!每一次递归返回的n中,需明白n指向返回的链表的头结点,这就是引入n的重要性 h和p仍为该循原创 2016-09-18 21:42:38 · 497 阅读 · 0 评论 -
147. Insertion Sort List (python)
Sort a linked list using insertion sort. 题意:采用插入排序法对链表进行排序 思路:类似于扑克牌按照从小到大插入,将第i个元素与左边已排序的元素比较,找到位置插入即可 排序方法可参考:http://blog.csdn.net/yang_yulei/article/details/27237641#comments 数组是将比要插入的元素大的数值往后移,原创 2016-09-19 22:11:41 · 1117 阅读 · 0 评论 -
19. Remove Nth Node From End of List(python)
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li原创 2016-09-18 21:30:12 · 359 阅读 · 0 评论 -
21. 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. 题意:按顺序拼接两个已排序的链表 runtime:76ms 记住虚表头dummy设定好了千万不要动,设定一个p=原创 2016-09-18 21:32:17 · 1997 阅读 · 0 评论 -
24. Swap Nodes in Pairs(python)
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You ma原创 2016-09-18 21:33:51 · 442 阅读 · 0 评论 -
83. 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. 题意:排序的链表删除重复的元素,保证每个元素出现一次原创 2016-09-18 21:34:35 · 1080 阅读 · 0 评论 -
141. Linked List Cycle(python)
Given a linked list, determine if it has a cycle in it. runtime:92ms 总结:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-linked list.# class ListNode(object):# def __init__(self原创 2016-09-18 21:36:20 · 460 阅读 · 0 评论 -
160. 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 ↘原创 2016-09-18 21:38:02 · 990 阅读 · 0 评论 -
203. Remove Linked List Elements(python)
Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 Runtime: 156 ms 总结:建立虚表头,比较p.next,如果p.原创 2016-09-18 21:39:48 · 975 阅读 · 0 评论