Do a few Leetcode array questions

Source: Internet
Author: User
Tags time limit
1. Remove duplicates from Sorted ArrayTotal accepted:29864 Total Submissions:91835my Submissions

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

Do not allocate extra spaces for another array, and you must does this in place with constant memory.

For example,
Given input Array A = [1,1,2],

Your function should return length = 2, and A are now [1,2].


Solution 1:

#include <iostream>

using namespace Std;

int removeduplicates (int a[], int n)

{

for (int i = 0; i < n; i++)

{

if (a[i] = = a[i + 1])

{

for (int j = i + 1; j < N. J + +)

A[J] = a[j + 1];

n--;

}

}

return n;

}


int main ()

{

int a[3] = {1,1,2};

cout<< "The result:" << removeduplicates (a,3) << Endl;

return 0;

}

When submitting, submission Result:time Limit exceeded

Later, all kinds of mistakes, finally change forward a thought

#include <iostream>

using namespace Std;

int removeduplicates (int a[], int n)

{

int j = 1;

if (n = = 0) j = 0;

for (int i = 1; i < n; i++)

{

if (A[i]!= a[j-1])

{

A[j++] = A[i];

}

}

Return J;

}

int main ()

{

int a[] = {};

cout<< "The result:" << removeduplicates (a,0) << Endl;

return 0;

} submission result:accepted
2. Is the extension of question 1 Remove duplicates from Sorted Array II total accepted:22371 Total submissions:72955my Submissions

Follow up for "Remove duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A are now [1,1,2,2,3].

Because of the last experience, AC

#include <iostream>

using namespace Std;

int removeduplicates (int a[], int n)

{

int j = 2;

if (n <= 2) return n;

for (int i = 2; i < n; i++)

{

if (A[i]!= a[j-2])

{

A[j++] = A[i];

}

}

Return J;

}

int main ()

{

int a[] = {1,1,1,2,2,3};

cout<< "The result:" << removeduplicates (a,6) << Endl;

return 0;

}


3. Search in rotated Sorted Array

Suppose a sorted array is rotated in some pivot unknown to your beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are are given a target value to search. If found in the "array return" its index, otherwise return-1.

You may assume no duplicate exists in the array.

Answer:

#include <iostream>

using namespace Std;

int search (int a[], int n, int target)

{

int head,tail,mid;

Head = 0;

tail = n;

while (Tail > Head)//tail did not subtract 1 without 〉=, or remember head = 0;tail = n-1; while (tail>=head) ... tail = mid-1; Head = Mid + 1 version bar, that is, the third method

{

Mid = (head + tail)/2; Divided by 2, you can also move the right way, multiplying by 2 to the left

if (a[mid] = = target) return mid;

if (A[head] <= A[mid])

{

if (A[mid] >= target && A[head] <= target)

tail = mid;

else Head = mid + 1; Do not add 1 will fall into the death cycle [4], find 5, where the mid as head, so mid must have been detected, head take a value forward plus one on the good

Here is a question is why the head of the update to add 1 and tail not, because division will only be in the head, will appear mid is equal to the head of the infinite cycle, or add 1 can also see below, as for tail think-1 words can be, after all, can be less to judge a number, See also below with 1 and minus 1 version

}

Else

{

if (A[tail-1] >= target && A[mid] <= target)//tail If you don't subtract 1, it crosses the line.

Head = mid + 1;

else tail = mid;

}

}

return-1;

}

int main ()

{

int a[] = {4};

cout<< "The result:" <<search (a,1,5) << Endl;

return 0;

}

2) A little change of the solution:

int head,tail,mid;

Head = 0;

tail = n;

Mid = (head + tail)/2; Over here

while (Mid > Head)

{

if (a[mid] = = target) return mid;

if (A[head] <= A[mid])

{

if (A[mid] >= target && A[head] <= target)

tail = mid;

else head = Mid; Here does not add 1, but while the judgment condition changed, so while on the first let mid take the value, but also afraid of missing the right head, so the back is added

}

Else

{

if (A[tail-1] >= target && A[mid] <= target)

Head = Mid;

else tail = mid;

}

Mid = (head + tail)/2; Changed to this place.

}

if (a[mid] = = target) return mid; Added this sentence to prevent omission

return-1;


3) plus 1 and minus 1 versions

int search (int a[], int n, int target)

{

int head,tail,mid;

Head = 0;

tail = n-1;

if (tail = 0)

if (a[head] = = target) return 0;

while (tail >= head)//Here 〉= is only greater than the case where the TAIL-1 = head is leaking,

{

Mid = (head + tail)/2;

if (a[mid] = = target) return mid;

if (A[head] <= A[mid])

{

if (A[mid] >= target && A[head] <= target)

tail = mid-1;

else Head = mid + 1;

}

Else

{

if (A[tail] >= target && A[mid] <= target)

Head = mid + 1;

else tail = mid-1;

}

}

return-1;

}


4. On the basis of 3

Follow up for ' Search in rotated Sorted Array ':
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given the target being in the array.


Class Solution {

Public

BOOL Search (int a[], int n, int target) {

int head,tail,mid;

Head = 0;

tail = n-1;

if (tail = 0)

if (a[head] = = target) return true;

while (tail >= head)

{

Mid = (head + tail)/2;

if (a[mid] = = target) return true;

if (A[head] < A[mid])

{

if (A[mid] >= target && A[head] <= target)

tail = mid-1;

else Head = mid + 1;

}

else if (A[head] > A[mid])

{

if (A[tail] >= target && A[mid] <= target)

Head = mid + 1;

else tail = mid-1;

}

else ++head;

}

return false;

}

};



5.Median of two Sorted Arrays

There are two sorted arrays A and B of size m and N respectively. Find the median of the two sorted arrays. The overall run time complexity should is O (log (m+n)).

This question has not been made out, the answer to the reference

Analysis:

Median and sorted--〉 find the number of specific positions---the odd-even two cases--〉 write the first function

Find the number of a location and sort----Compare the length--〉 of two sequences in two cases (merge for easy analysis)----Two array interception (considering boundary conditions)



Class Solution {

Public

Double findmediansortedarrays (int a[], int m, int b[], int n) {

int total = m + N;

if (Total & 0x1)//Judge parity, more efficient than%2 (in-depth understanding of computer systems also mentioned)

Return find_kth (A,M,B,N,TOTAL/2 + 1);

Else

Return (find_kth (A,M,B,N,TOTAL/2) + find_kth (A,M,B,N,TOTAL/2 + 1))/2;

}

static double find_kth (int a[],int m, int b[], int n, int k)

{

if (M > N) return find_kth (B,N,A,M,K);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.