算法课作业
文章平均质量分 59
luoyingmin
这个作者很懒,什么都没留下…
展开
-
260. Single Number III 难度:medium
题目:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:原创 2016-12-29 14:42:36 · 297 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree 难度:easy
题目:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is def原创 2017-01-06 21:51:10 · 232 阅读 · 0 评论 -
77. Combinations 难度:medium
题目:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3],原创 2017-01-07 10:47:16 · 250 阅读 · 0 评论 -
334. Increasing Triplet Subsequence 难度:medium
题目:Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, k such that原创 2017-01-07 11:38:07 · 154 阅读 · 0 评论 -
367. Valid Perfect Square 难度:medium
题目:Given a positive integer num, write a function which returns True if num is a perfect square else False.Note: Do not use any built-in library function such as sqrt.Example 1:Input:原创 2017-01-07 11:47:57 · 166 阅读 · 0 评论 -
27. Remove Element 难度:easy
题目: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 me原创 2017-01-07 12:01:07 · 145 阅读 · 0 评论 -
66. Plus One 难度:easy
题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.原创 2017-01-07 12:32:09 · 221 阅读 · 0 评论 -
75. Sort Colors 难度:medium
题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the原创 2017-01-07 12:41:46 · 184 阅读 · 0 评论 -
118. Pascal's Triangle 难度:easy
题目: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个位置,等于原创 2017-01-07 12:44:24 · 178 阅读 · 0 评论 -
263. Ugly Number 难度:easy
题目:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not原创 2017-01-06 21:35:41 · 199 阅读 · 0 评论 -
191. Number of 1 Bits 难度:easy
题目:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representati原创 2017-01-06 21:31:12 · 183 阅读 · 0 评论 -
453. Minimum Moves to Equal Array Elements 难度:easy
题目:Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1原创 2016-12-29 15:00:13 · 120 阅读 · 0 评论 -
383. Ransom Note 难度:easy
题目: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 magaz原创 2016-12-29 15:09:39 · 190 阅读 · 0 评论 -
349. Intersection of Two Arrays 难度:easy
题目:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].程序:class Solution {public: vector intersecti原创 2016-12-29 16:36:46 · 154 阅读 · 0 评论 -
387. First Unique Character in a String 难度:easy
题目:Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.思路原创 2016-12-29 16:45:02 · 143 阅读 · 0 评论 -
386. Lexicographical Numbers 难度:medium
题目:Given an integer n, return 1 - n in lexicographical order.For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].思路:优先将数字乘10;如果数字末位<9,考虑将数字加1。程序:class Solution {p原创 2017-01-06 20:02:20 · 219 阅读 · 0 评论 -
202. Happy Number 难度:easy
题目:Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of th原创 2017-01-06 20:14:12 · 183 阅读 · 0 评论 -
199. Binary Tree Right Side View 难度:medium
题目:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree原创 2017-01-06 21:17:13 · 179 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array
题目:Suppose 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).Find the minimum element.思路:用二分法原创 2017-01-06 21:26:24 · 155 阅读 · 0 评论 -
100. Same Tree 难度:easy
题目:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思原创 2016-12-30 21:02:06 · 148 阅读 · 0 评论 -
242. Valid Anagram 难度:easy
题目:Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.思路:原创 2016-12-30 21:04:36 · 147 阅读 · 0 评论 -
217. Contains Duplicate 难度:easy
题目: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 ele原创 2016-12-30 21:58:37 · 158 阅读 · 0 评论 -
70. Climbing Stairs 类别:动态规划 难度:easy
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:类似斐波那契序列。原创 2017-01-08 22:12:56 · 233 阅读 · 0 评论 -
198. House Robber 类别:动态规划 难度:easy
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjac原创 2017-01-08 22:15:00 · 233 阅读 · 0 评论 -
416. Partition Equal Subset Sum 类别:动态规划 难度:medium
题目:Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of原创 2017-01-08 22:17:56 · 829 阅读 · 0 评论 -
64. Minimum Path Sum 类别:动态规划 难度:medium
题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.思路:转移方程:dp[i][j] = min(grid[m -原创 2017-01-08 22:19:40 · 238 阅读 · 0 评论 -
376. Wiggle Subsequence 类别:动态规划 难度:medium
题目:A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be e原创 2017-01-08 22:24:10 · 289 阅读 · 0 评论 -
算法概论8.15 最大公共子图
题目:证明如下问题是NP-完全的:输入:两个图G1 = (V1,E1),G2 = (V2,E2);预算b。输出:两个节点集合V1‘⊆V1,V2’⊆V2,将它们移除后,将在两图中分别留下至少b个结点,且图的剩余部分完全相同。解答:我们已知求给定图的最大独立集问题是NP完全问题,由此可以将最大独立集问题归约到此问题。假设给定的G1 = (V,E),G2 =原创 2017-01-13 21:59:52 · 2423 阅读 · 0 评论 -
477. Total Hamming Distance 难度:medium
题目:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs o原创 2017-01-01 17:55:42 · 190 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix 难度:medium
题目:Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted o原创 2017-01-01 18:14:28 · 215 阅读 · 0 评论 -
62. Unique Paths 类别:动态规划 难度:medium
题目:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to r原创 2017-01-08 22:11:13 · 247 阅读 · 0 评论 -
343. Integer Break 类别:动态规划 难度:medium
题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n原创 2017-01-08 22:08:49 · 304 阅读 · 0 评论 -
350. Intersection of Two Arrays II 难度:easy
题目:Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].Note:Each element in the result should app原创 2016-12-30 22:19:59 · 150 阅读 · 0 评论 -
268. Missing Number 难度:medium
题目:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.思路:等差数列求和,再逐个减去数原创 2016-12-30 22:25:28 · 151 阅读 · 0 评论 -
461. Hamming Distance 难度:easy
题目:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:原创 2017-01-08 20:53:45 · 165 阅读 · 0 评论 -
419. Battleships in a Board 难度:medium
题目:Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:Y原创 2017-01-08 20:58:52 · 159 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array 难度:easy
题目:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this a原创 2017-01-08 21:04:03 · 163 阅读 · 0 评论 -
463. Island Perimeter 难度:easy
题目: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 co原创 2017-01-08 21:09:27 · 189 阅读 · 0 评论 -
442. Find All Duplicates in an Array 难度:medium
题目:Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it w原创 2017-01-08 21:34:23 · 217 阅读 · 0 评论 -
357. Count Numbers with Unique Digits 类别:动态规划 难度:medium
题目:Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x [11,原创 2017-01-08 22:06:14 · 240 阅读 · 0 评论