您的位置:首页 > 职场人生

面试笔试杂项积累-leetcode 56-60

2016-01-31 22:54 429 查看

56.56-Merge Intervals-Difficulty:Hard

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]
.

思路

查找数组集中是否有互相包含的数组,使之"融合"后返回数组集。"融合"->调整那个数组的范围即可。

注意:

要做“叠”与“被叠”两次判断

还有“包含”与“被包含”关系需要处理

很简单,循环判断即可

/**
* Definition for an interval.
* public class Interval {
*     public int start;
*     public int end;
*     public Interval() { start = 0; end = 0; }
*     public Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public IList<Interval> Merge(IList<Interval> intervals) {
List<Interval> temp = new List<Interval>(intervals);
int length = temp.Count;
for (int i = 0; i < length-1; i++)
{
for (int j = i+1; j < length; j++)
{
if (i != j)
{
if (temp[i].end >= temp[j].start && temp[i].start <= temp[j].start)
{
if (temp[i].end >= temp[j].end && temp[i].start <= temp[j].start)
{
temp.RemoveAt(j);
--length;
--i;
break;
}
temp[i].end = temp[j].end;
temp.RemoveAt(j);
--length;
--i;
break;
}
if (temp[j].end >= temp[i].start && temp[j].start <= temp[i].start)
{
if (temp[j].end >= temp[i].end && temp[j].start <= temp[i].start)
{
temp.RemoveAt(i);
--length;
--i;
break;
}
temp[j].end = temp[i].end;
temp.RemoveAt(i);
--length;
--i;
break;
}
}
}

}
return temp;
}
}


57.57-Insert Interval-Difficulty:Hard

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],[3,5],[6,7],[8,10],[12,16]
, insert and merge
[4,9]
in as
[1,2],[3,10],[12,16]
.

This is because the new interval
[4,9]
overlaps with
[3,5],[6,7],[8,10]
.

思路

与上一道56题差不多,也是“融合”的问题,只不过这次是要插入一个数组,判断是否有互相包含的数组,有即"融合",没有插入到数组集中。

要注意:

1.不能直接插入或删除传入数组集的值,因为传入变量的length is fixed。故深复制一个来操作

2.融合之后还有可能依旧与数组集中的数组互相包含,所以要重头判断检查。

3.要做“叠”与“被叠”两次判断

4.还有“包含”与“被包含”关系需要处理

/**
* Definition for an interval.
* public class Interval {
*     public int start;
*     public int end;
*     public Interval() { start = 0; end = 0; }
*     public Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public IList<Interval> Insert(IList<Interval> intervals, Interval newInterval) {
IList<Interval> temp = new List<Interval>(intervals);
for (int i = 0; i < temp.Count; i++)
{
if (newInterval.end < temp[i].start)
{
temp.Insert(i,newInterval);
return temp;
}
if (newInterval.start >= temp[i].start && newInterval.end <= temp[i].end)
{
return temp;
}
if (newInterval.start <= temp[i].start && newInterval.end >= temp[i].end)
{
temp.RemoveAt(i);
if(i == temp.Count)
break;
i = -1;
continue;
}
if (newInterval.start <= temp[i].end && newInterval.start > temp[i].start)
{

newInterval.start = temp[i].start;
temp.RemoveAt(i);
i = -1;
continue;
}
if (temp[i].start <= newInterval.end && temp[i].start > newInterval.start)
{

newInterval.end = temp[i].end;
temp.RemoveAt(i);
i = -1;
continue;
}
}
temp.Add(newInterval);
return temp;
}
}


58.58-Length of Last Word-Difficulty:Easy

Given a string s consists of upper/lower-case alphabets and empty space characters
' '
, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,

Given s =
"Hello World"
,

return
5
.

思路

在连续做了几道hard之后,做easy的感觉简直是像小学的十位数以内的加减法啊。。无需多言。。。

题意:返回后面除了括号的最后一个单词长度

public class Solution {
public int LengthOfLastWord(string s) {
int temp = 0;
bool start = false;
for (int i = 0; i < s.Length; i++)
{
if (s[s.Length - 1 - i] != ' ')
{
if (!start)
start = true;
++temp;
}
else
{if(start) break; }
}
return temp;
}
}

59.59-Spiral Matrix II-Difficulty:Medium

Given an integer n, generate a square matrix filled with elements from 1 to
n2 in spiral order.

For example,

Given n =
3
,

You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

思路

给出行/列个数,返回顺序的螺旋方阵。思路与54题相同

public class Solution {
public int[,] GenerateMatrix(int n) {
int[,] fin = new int[n, n];
int state = 0;//0-上横,1-右竖,2-下横,3-左竖
int temp = 1;
int col_length = n;
int row_length = n;
int col_start = 0;
int row_start = 0;
for (int i = 0; i < n * n; i++)
{
switch (state)
{
case 0:
if (temp <= col_length)
{
fin[row_start, col_start + temp - 1] = i + 1;
++temp;
}
else
{
++row_start;
--row_length;
temp = 1;
++state;
--i;
}

break;
case 1:
if (temp <= row_length)
{
fin[row_start + temp - 1,col_start+ col_length - 1] = i + 1;
++temp;
}
else
{
temp = 1;
--col_length;
++state;
--i;
}
break;
case 2:
if (temp <= col_length)
{
fin[row_start+ row_length - 1, col_start + col_length - temp] = i + 1;
++temp;
}
else
{
--row_length;
temp = 1;
++state;
--i;
}
break;
case 3:

if (temp <= row_length)
{

fin[ row_start + row_length - temp,col_start] = i + 1;
++temp;
}
else
{
temp = 1;
state = 0;
++col_start;
--col_length;
--i;
}
break;
}

}

return fin;
}
}

60.60-Permutation Sequence-Difficulty:Medium

The set
[1,2,3,…,n]
contains a total of
n! unique permutations.

By listing and labeling all of the permutations in order,

We get the following sequence (ie, for n = 3):

"123"

"132"

"213"

"231"

"312"

"321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

思路

给一个序列,输出全排列中的第k个

参考:
http://www.programcreek.com/2013/02/leetcode-permutation-sequence-java/
public class Solution {
public string GetPermutation(int n, int k)
{
bool[] output = new bool
;
StringBuilder buf = new StringBuilder("");

int[] res = new int
;
res[0] = 1;

for (int i = 1; i < n; i++)
res[i] = res[i - 1] * i;

for (int i = n - 1; i >= 0; i--)
{
int s = 1;

while (k > res[i])
{
s++;
k = k - res[i];
}

for (int j = 0; j < n; j++)
{
if (j + 1 <= s && output[j])
{
s++;
}
}

output[s - 1] = true;
buf.Append(s.ToString());
}

return buf.ToString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: