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

面试笔试杂项积累-leetcode 286-290

2016-02-13 23:14 513 查看

287.287-Find the Duplicate Number-Difficulty: Hard

Given an array nums containing n + 1 integers where each integer is between 1 and
n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than
O(n2)
.
There is only one duplicate number in the array, but it could be repeated more than once.

方法一

思路

找到重复数字

蠢方法就是,排下序,然后遍历

public class Solution {
public int FindDuplicate(int[] nums) {
int[] temp = new int[nums.Length];
Array.Copy(nums, temp, nums.Length);
Array.Sort(temp);
for (int i = 1; i < temp.Length; i++)
{
if (temp[i] == temp[i - 1])
return temp[i];
}
return 0;
}
}

方法二

思路

二分搜索法

参考:
https://leetcode.com/discuss/74367/java-easy-version-to-understand
public static int findDuplicate(int[] nums) {
if (nums.length == 0 || nums == null)
return 0;
int low = 1, high = nums.length - 1, mid;
while (low < high) {
mid = low + (high - low) / 2;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] <= mid)
count++;
}
if (count > mid)
high = mid;
else
low = mid + 1;
}
return low;
}参考: https://leetcode.com/discuss/69766/share-my-solution-o-n-time-o-1-space-12-ms
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int slow = 0;
int fast = 0;
int finder = 0;

while (true)
{
slow = nums[slow];
fast = nums[nums[fast]];

if (slow == fast)
break;
}
while (true)
{
slow = nums[slow];
finder = nums[finder];
if (slow == finder)
return slow;
}
}

289.289-Game of Life-Difficulty: Medium

According to the
Wikipedia's article: "The Game of Life, also known simply asLife, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

Given a board with m by n cells, each cell has an initial statelive (1) or
dead (0). Each cell interacts with its
eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population..
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.

Follow up:

Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?

思路

给出一个m*n的细胞矩阵,每个细胞都有一个初始状态:生存(1)或死亡(0)。每个细胞的变化都与它周围8个细胞有关,规则如下:

当前细胞为存活状态时,当周围存活细胞不到2个时, 该细胞变成死亡状态。(模拟生命数量稀少)

当前细胞为存活状态时,当周围有2个或3个存活的细胞时, 该细胞保持原样。

当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)

当前细胞为死亡状态时,当周围恰好有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)

弄一个函数来判断周围的细胞个数,与是生是死,由于可以繁殖,所以0的位置也要判断,在函数中传入bool标记是否是死位置就好,3个邻居就变1
切记每次改变不影响下一次判断,深复制一个board专门用来判断

public class Solution {
public void GameOfLife(int[,] board) {
int row = board.GetLength(0);
int col = board.GetLength(1);
int[,] temp = new int[row, col];
Array.Copy(board, temp, board.Length);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
if (board[i, j] == 1)
{
if (!isLive(temp, i, j, true))
board[i, j] = 0;
}
else
{
if (isLive(temp, i, j, false))
board[i, j] = 1;
}
}
}
bool isLive(int[,] temp, int row, int col, bool isLiving)
{
int neighbor = 0;
if (row < temp.GetLength(0) - 1 && temp[row + 1, col] == 1)
++neighbor;
if (row > 0 && temp[row - 1, col] == 1)
++neighbor;

if (row < temp.GetLength(0) - 1 && col < temp.GetLength(1) - 1 && temp[row + 1, col + 1] == 1)
++neighbor;
if (row < temp.GetLength(0) - 1 && col > 0 && temp[row + 1, col - 1] == 1)
++neighbor;

if (row > 0 && col < temp.GetLength(1) - 1 && temp[row - 1, col + 1] == 1)
++neighbor;
if (row > 0 && col > 0 && temp[row - 1, col - 1] == 1)
++neighbor;

if (col < temp.GetLength(1) - 1 && temp[row, col + 1] == 1)
++neighbor;
if (col > 0 && temp[row, col - 1] == 1)
++neighbor;
if (isLiving)
{
if (neighbor == 2 || neighbor == 3)
return true;
}
else
{
if (neighbor == 3)
return true;
}
return false;
}
}


290.290-Word Pattern-Difficulty: Medium

Given a
pattern
and a string
str
, find if
str
follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in
pattern
and a
non-empty word in
str
.

Examples:

pattern =
"abba"
, str =
"dog cat cat dog"
should return true.
pattern =
"abba"
, str =
"dog cat cat fish"
should return false.
pattern =
"aaaa"
, str =
"dog cat cat dog"
should return false.
pattern =
"abba"
, str =
"dog dog dog dog"
should return false.
Notes:

You may assume
pattern
contains only lowercase letters, and
str
contains lowercase letters separated by a single space.

思路

使用哈希表映射

新手问题,判断string是否相同可以用equal和contain两个函数

public class Solution {
public bool WordPattern(string pattern, string str) {
Hashtable hash = new Hashtable();
int start = 0;
int cur = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ' ' || i == str.Length - 1)
{
string temp = str.Substring(start, i - start);
if (i == str.Length - 1)
temp = str.Substring(start);
if (cur >= pattern.Length)
return false;
if (hash.ContainsKey(pattern[cur]))
{
if (!temp.Equals((string)hash[pattern[cur]]))
return false;

}
else
{
if (hash.ContainsValue(temp))
return false;
hash.Add(pattern[cur], temp);
}
++cur;

start = i + 1;
}

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