- 博客(27)
- 资源 (4)
- 收藏
- 关注
原创 389. Find the Difference
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot
2017-04-28 23:33:13 253
原创 118. Pascal's Triangle
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] ] 题意:打印杨辉三角形 思路:用list辅助存储,写
2017-04-27 09:24:36 466
原创 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 /
2017-04-27 08:55:24 652
原创 169. 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 majority element always
2017-04-26 13:55:35 265
原创 172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Credits:Special thanks to @ts for adding this problem and creating all test c
2017-04-19 20:31:58 264
原创 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word
2017-04-19 15:43:15 666
原创 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.
2017-04-19 08:56:36 466
原创 292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the
2017-04-19 08:51:39 482
原创 463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely s
2017-04-18 11:17:28 262
原创 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be unique.The result can be in
2017-04-17 19:04:53 433
原创 496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2. Th
2017-04-17 10:19:39 318
原创 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6Return: 1 –> 2 –> 3 –> 4 –> 5Credits:Special thanks to @mithmatt for add
2017-04-16 16:49:16 374
原创 435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note:You may assume the interval’s end point is always bigge
2017-04-16 16:02:20 329
原创 Remove Duplicates系列笔记
第一题Python代码:# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def deleteDuplicates(self, h
2017-04-14 10:43:13 2007
原创 226. Invert Binary Tree
题目大意: 翻转二叉树思路:递归一下,递归比迭代好写Python代码:# Definition for a binary tree node.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass
2017-04-13 23:19:01 256
原创 206. Reverse Linked List
题目大意: 反转一个链表,要求分别用迭代和递归实现 Python代码:# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def
2017-04-13 16:17:23 283
原创 290. Word Pattern
题目大意: 给定两个字符串pattern和str,判断str的模式是否和pattern一直思路: 一开始没什么思路,直接写了两层for循环,O(n2)的代码辣眼睛,提交坐等超时,然而并没有超时,持续懵逼中。Python代码 arr = str.split(' ') print(arr) if len(pattern) != len(arr):
2017-04-13 09:36:45 522
原创 73. Set Matrix Zeroes
题目大意: 给出一个m * n的矩阵,如果矩阵中的某个元素为0,则将该元素所在的整行和整列设为0,要求空间复杂度为O(1),举个栗子: [0,1,2,3] [0,0,0,0] [4,5,6,7] -> [0,5,6,7] [8,9,1,2] [0,9,1,2] 思路: 如果放宽对空间复杂度的限制,可以将原始矩阵复制一份,一边遍历原始矩阵,一边修改复制矩阵,这样
2017-04-13 09:15:31 372
原创 204. Count Primes
题目大意: 给一个非负整数n,求小于n的素数的个数思路: 第一反应是采用素数的定义来做。(1)如果采用素数的定义,即不能被1和它本身整除,需要从2计算到它减一,举个栗子,如果要验证17是素数,则需要计算17 / 2,17 / 3,17 / 4 … 17 / 16,O(n)级别的复杂度。伪代码如下:for(int i = 2; i < n; i++) { // 此处是判断n是否可以被i整除
2017-04-12 11:37:18 322
原创 451. Sort Characters By Frequency
题目大意: 按照字符串中的字符出现次数排序,注意大写和小写字符要区分开。一道有点麻烦的题,用一个Map存储字符及出现的次数,然后重写comparator对Map进行排序。 Java代码如下:import java.util.*;import java.util.Map.Entry;public class Solution { public List<Map.Entry<Charact
2017-04-11 17:31:54 315
原创 21. Merge Two Sorted Lists
题目大意: 有两个已经排好序的链表,现在要合并这两个链表,同时保证大小顺序不变,返回合并后的链表,如:1->3->5和1->4合并后为:1->1->3->4->5思路: 令i,j分别为两个链表的起始节点,选择值较小的那个节点,加入到返回链表中,此处i指向的节点的值为1,j指向节点的值为1,两个值相等,(相等时默认加入i),此处把 i 指向的节点加入到返回链表中,随后 i 后移一位。 之后,i指
2017-04-11 17:28:09 540
原创 237. Delete Node in a Linked List
题目大意: 有一个链表如:1->2->3->4->5,要删除3这个节点,求删除后的链表思路:题目没有给出链表的头结点,也就获取不了待删除点的前驱结点,因此采用复制后一个元素的值并替代前一个元素值的方式,实现元素的删除 Python代码:# Definition for singly-linked list.# class ListNode(object):# def __init
2017-04-11 09:10:56 251
原创 堆排序
堆排序的时间复杂度O(nlogn),空间复杂度O(1)堆(或二叉堆),类似于完全二叉树,除叶子节点外,每个节点均拥有左子树和右子树,同时左子树和右子树也是堆。 小顶堆:父节点的值 <= 左右孩子节点的值 大顶堆:父节点的值 >= 左右孩子节点的值堆的存储: 用一个数组存储堆就可以了,如[19, 17, 20, 18, 16, 21] 对于数组中的第 i 个节点(从0开始),有如下规律
2017-04-10 22:25:36 373
原创 120. Triangle
题目大意: 有一个三角形矩阵,从最上边的顶点出发累加到底边,一共有多种情况,求这些情况中累加和的最小值 要求额外空间复杂度为O(n),n是行数思路: 遍历矩阵,每走一步都更新当前值: triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j]) Python代码如下:class Solution(object): def m
2017-04-10 14:41:20 426
原创 62. Unique Paths
题意大致为: 一个机器人从网格的左上角Start,走到右下角Finish,则不同路径的走法有多少种。输入是网格的行、列值。 这是一道典型的动态规划题,首先,初始化一个m x n的数组arr,(i,j)位置上记录走到(i,j)时的走法数。 第一行均初始化为1,因为对于第一行的元素而言,想要走到它,只能是从左一路走过来。 第一列均初始化为1, 因为对于第一列的元素而言,想要走到它,只能是从上一路
2017-04-07 16:48:14 615 1
原创 19. Remove Nth Node From End of List
题意如下: 给定一个链表和整数n,删除链表中的倒数第n个元素 例如: 1 -> 2 -> 3 -> 4 -> 5, n = 2 则删除4,返回1 -> 2 -> 3 -> 5 要求只能遍历一遍列表我的思路如下: 用到三个指针:pre、p、q。pre指向p的前一个元素;p指向待删除的元素;q指向链表中最后一个元素; 起初,p指向链表头结点head,q从头结点head向后移动到第n位。然后
2017-04-07 10:15:34 278
原创 448. Find All Numbers Disappeared in an Array
题意如下:对于一个长度为n的数组,其内部元素均由1 ~ n组成,有些元素重复出现了2次,有些元素出现了1次,有些元素从未出现,求那些从未出现的元素。要求:额外空间复杂度为O(1), 时间复杂度为O(n)解法:如果不考虑空间复杂度和时间复杂度,可以使用两层循环,统计每一个元素在数组中出现的次数,优点是好写,但是当数据量大时容易超时。标志位法:用正负标志位,区分出现的元素和
2017-04-07 09:48:42 684
python写的列主元Gauss消去法
2015-09-23
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人