- 浏览: 184786 次
- 性别:
- 来自: 济南
最新评论
文章列表
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
Solution:
class Solution(object):
def findDiagonalOrder(self, ...
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.
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If ...
How to upload your code to git server?
After installing git, execute these in command line:
git config --global user.name "Your Name"
git config --global user.email "email@example.com
*******
/etc/gitconfig and ~/.gitconfig.
each level overrides values in the previous level.
.gitco ...
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", " ...
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2], ...
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
题目中给定了一个interval集合,要求我们将集合中的interval合并,合并之后的interval是按照升序排列的。我们可以先将interval排序先按照interval的第一个元素,如果第一个元素相等就按照第二个元素。排序的时候我们定义一个比较器comparator。排序之后,从第二个interval开始 ...
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Merge k个有序链表,之前做过一道题目是merge两个有序的链表,这里我们用分治的思想,用归并排序解决。先将k个链表递归的分为两部分,直到剩下两个链表,然后将两个链表合并起来。因为有k个list,假设每个list中有n个元素,那么时间复杂度为O(nk*logk). 代码如下:
/**
* Definition for singly-linked list.
* public clas ...
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
计算两个大数的乘法,如果直接计算,就会溢出。假设两个数的长度分别为m, n。那么它们相乘之后的长度肯定为m+n(有进位), 或m + n - 1(没有进位)。我们建立一个长度为m + n 的数组,用来求解每一位上对应的值,维护一个进位。代码如下:
public class Sol ...
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
同样是N皇后的问题,要求我们输出有几种结果,思路同N-Queens是一样的,这里就不在赘述了,代码如下:
public class Solution {
private int result = 0;
public int totalNQueens(int n) {
int[] colInRow = n ...
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both i ...
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
用到了桶排序,我们让第i个位置放值为i + 1的元素。当nums[i]不等于i + 1时,就让nums[nums[i] - 1]] = nums[i], 这样nums[nums[i] - 1]的位 ...
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
给定一个m*n个矩阵,按照顺时针的方向,输出矩阵中的元素。设定四个变量,top, bottom, left, right, 从第一行第一 ...
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 ...
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once ...
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 arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) ti ...