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

面试笔试杂项积累-leetcode 51-55

2016-01-31 22:23 337 查看

53.53-Maximum Subarray-Difficulty:Medium

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array
[−2,1,−3,4,−1,2,1,−5,4]
,

the contiguous subarray
[4,−1,2,1]
has the largest sum =
6
.

思路

找出数组中连续数的最大和,返回最大和。两个循环来比较,发现叠加小于0的全不要break;下一组连续数比较。

public class Solution {
public int MaxSubArray(int[] nums) {
if (nums.Length < 2)
return nums[0];
int fin = nums[0];
int temp = 0;
for (int i = 0; i < nums.Length - 1; i++)
{
temp = nums[i];
if (nums[i] > fin)
{
fin = nums[i];
}
for (int j = i + 1; j < nums.Length; j++)
{
temp = temp + nums[j];
if (temp < 0)
break;
if (temp >= fin)
{
fin = temp;
}
}
}
if (nums[nums.Length - 1] > fin)
{
fin = nums[nums.Length - 1];
}
return fin;

}
}

54.54-Spiral Matrix-Difficulty:Medium

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

思路

螺旋数组问题,给一个矩阵返回螺旋数组。前几个月在程序员面试宝典上看过这道题,有很机智的解法。此处先上博主的方法,分四种状态,分别为上面横、右面竖、下面横、左面竖,在一个大循环length中进行,一个状态完了就进入另一个状态,并对矩阵行列起始位置、长度进行修改

public class Solution {
public IList<int> SpiralOrder(int[,] matrix) {
List<int> fin = new List<int>();
int row_length = matrix.GetLength(0);//行数
int col_length = matrix.GetLength(1);//列数
if (row_length < 1 || col_length < 1)
return fin;
if (row_length == 1)
{ for (int i = 0; i < col_length; i++) { fin.Add(matrix[0, i]); } return fin; }
if(col_length == 1)
{ for (int i = 0; i < row_length; i++) { fin.Add(matrix[i, 0]); } return fin; }
if (col_length == 2 && row_length == 2)
{ fin.Add(matrix[0, 0]); fin.Add(matrix[0, 1]); fin.Add(matrix[1, 1]); fin.Add(matrix[1, 0]); return fin; }
int row_start = 0;
int col_start = 0;
int state = 0;//0-上,1-右,2-下,3-左
int temp = 1;
for (int i = 0; i < matrix.Length; i++)
{
switch (state)
{
case 0:
if (temp <= col_length)
{
fin.Add(matrix[row_start, row_start+(temp - 1 )]);//上横
++temp;
}
else
{
++row_start;
--row_length;
temp = 1;
++state;
--i;
}
break;
case 1:
if (temp <= row_length)
{
fin.Add(matrix[row_start+(temp - 1 ) , col_start + col_length - 1]);//右竖
++temp;
}
else
{
--col_length;
temp = 1;
++state;
--i;
}
break;
case 2:
if (temp <= col_length)
{
fin.Add(matrix[row_start + row_length - 1, col_start + col_length - (temp - 1)-1]);//下横
++temp;
}
else
{
--row_length;
temp = 1;
++state;
--i;
}
break;
case 3:
if (temp <= row_length)
{
fin.Add(matrix[col_start + row_length - (temp - 1) % row_length, col_start]);//左竖
++temp;
}
else
{
--col_length;
++col_start;
temp = 1;
state = 0;
--i;
}
break;
}
}
return fin;
}
}

55.55-Jump Game-Difficulty:Hard

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:

A =
[2,3,1,1,4]
, return
true
.

A =
[3,2,1,0,4]
, return
false
.

思路

从0开始,最大能跳该位置的数多少的步数,判断能否跳到最后一个数。

45题有一个jump GameII,思路和它差不多(= =..不知道为什么2在1前面)。区别就是这次不一定能跳到终点。

如果整个数组都大于0,那么肯定能到达终点,故作此判断。如果存在0,则采用45题的方法,最大跳跃,如果跳跃到0值那么就跳不到终点返回false

public class Solution {
public bool CanJump(int[] nums) {
if (nums.Length == 0)
return false;
int i = 0;
//判断有没有0,没有0的肯定能达到
while (i < nums.Length)
{
if (nums[i] == 0)
{
break;
}
i++;
}
//没有0,肯定能达到
if (i == nums.Length)
{
return true;
}
i = 0;
while (i < nums.Length)
{
if (i + nums[i] >= nums.Length - 1)
return true;
if (nums[i] == 0)
return false;
int max = 0;
int index = 0;
//下一步能前进最大的步骤
for (int j = i + 1; j - i <= nums[i]; j++)
{
if (max < j - i + nums[j])
{
max = j - i + nums[j];
index = j;
}
}//走到下一步的索引
i = index;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: