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

面试笔试杂项积累-leetcode 26-30

2016-01-29 13:35 531 查看

26.26-Remove Duplicates from Sorted Array-Difficulty: Easy

Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,

Given input array nums =
[1,1,2]
,

Your function should return length =
2
, with the first two elements ofnums being
1
and
2
respectively. It doesn't matter what you leave beyond the new length.

思路

题目要求给出一个数组,返回删去重复值之后的长度,并在传入数组中的该长度前的数组为删去重复数后的数组。

判断之后把不重复的数堆到前面就行了,很简单,时间复杂度O(n)

public class Solution {
public int RemoveDuplicates(int[] nums) {
if (nums.Length == 0)
return 0;
int temp = 1;
for (int i = 1; i < nums.Length; i++)
{
if (nums[i - 1] == nums[i])
{
continue;
}
else
{
if (temp < i)
{
nums[temp] = nums[i];
}
++temp;
}
}
return temp;
}
}

27.27-Remove Element-Difficulty: Easy

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

思路

题意为给出一数组和一个数,删除数组中等于该数的数,返回长度,,并在传入数组中的该长度前的数组为删去重复数后的数组。

思路与算法全部与上一道题(26)相同,改一下判断条件即可,时间复杂度O(n)

public class Solution {
public int RemoveElement(int[] nums, int val) {
if (nums.Length == 0)
return 0;
int temp = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] == val)
{
continue;
}
else
{
if (temp < i)
{
nums[temp] = nums[i];
}
++temp;
}
}
return temp;
}
}

28.28-Implement strStr()-Difficulty: Easy

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路

给定两个string,返回haystack中第一个等于needle 的位置,如果没有则返回-1。很简单,循环判断即可。

注意:位置是程序员的位置,你懂得,0是1,1是0。

让博主感到神奇的是,空值与空值匹配的第一个位置是0。。这个真没想到= =。。。

public class Solution {
public int StrStr(string haystack, string needle) {
if (needle.Length <= 0)
return 0;
int i = 0;
int j = 1;
for (; i < haystack.Length - needle.Length+1; i++)
{
if (haystack[i] == needle[0])
{
if (needle.Length == 1)
return i;
for (j = 1; j < needle.Length; j++)
{
if (haystack[j+i] != needle[j])
break;
else
{
if (j == needle.Length - 1)
return i;
}
}
}
}
return -1;
}
}

29.29-Divide Two Integers-Difficulty:Medium

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

思路

题目要求给出两个数相除的结果,不能使用乘法,除法和mod取余运算。当然是使用减法,博主开始尝试挨个减然后就这样



没有考虑单纯这样减多么浪费。上网发现一个比较好的用减法的方法,把减去的值累加起来减,减少很多时间

另外要注意正负值以及超界问题

其他方法

在Difficulty Distribution 中,这道题的解决方法有Binary Search 和 Math的两种方法。上面这种算Math了,在网上又看到有位运算的解决方法,很巧妙

public class Solution {
public int Divide(int dividend, int divisor) {
if (divisor == 0 || dividend == 0)
{
return 0;
}
if (dividend == int.MinValue&&divisor==-1)
++dividend;
bool negative = (dividend < 0) ^ (divisor < 0);

long a = abs(dividend);
long b = abs(divisor);

int finalCount = 0;
int count = 0;

while (a >= b)
{
count = 1;
b = abs(divisor);
long sum = b;
while (sum + sum <= a)
{
sum += sum;
count += count;
}
a -= sum;
finalCount += count;
};
if (negative)
{
return 0 - finalCount;
}
else
{
return finalCount;
}
}
long abs(int num)
{
if (num < 0)
{
return -(long)num;
}
return (long)num;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: