Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
C语言
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) {
*returnSize=0;
for(int i=0;i<numsSize;i++){
int m=abs(nums[i])-1;
if(nums[m]>0){
nums[m]=-nums[m];
(*returnSize)++;
}
}
*returnSize = numsSize-(*returnSize);
int *res=(int *)malloc(sizeof(int) * (*returnSize));
int k=0;
for(int i=0;i<numsSize;i++){
if(nums[i]>0){
res[k]=i+1;
k++;
}
}
return res;
}
Success
Details
Runtime: 84 ms, faster than 51.05% of C online submissions for Find All Numbers Disappeared in an Array.
Memory Usage: 18.4 MB, less than 100.00% of C online submissions for Find All Numbers Disappeared in an Array.
python3
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
seen = [0] * (len(nums) + 1)
for i in nums:
seen[i] = 1
return [i for i in range(1,len(nums)+1) if not seen[i]]
Success
Details
Runtime: 140 ms, faster than 92.59% of Python3 online submissions for Find All Numbers Disappeared in an Array.
Memory Usage: 18.7 MB, less than 46.08% of Python3 online submissions for Find All Numbers Disappeared in an Array.
Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
C语言
int findMaxConsecutiveOnes(int* nums, int numsSize) {
int i=0,count=0,max=0;
while(i<numsSize){
if(nums[i]==1){
count+=1;
if(count>max){max=count;}
}
else{count=0;}
i++;
}
return max;
}
Success
Details
Runtime: 16 ms, faster than 92.33% of C online submissions for Max Consecutive Ones.
Memory Usage: 8.6 MB, less than 77.78% of C online submissions for Max Consecutive Ones.
python3
分割思想
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
a=(''.join(map(str,nums))).split('0')
return max(map(len,a))
Success
Details
Runtime: 88 ms, faster than 53.93% of Python3 online submissions for Max Consecutive Ones.
Memory Usage: 14 MB, less than 6.50% of Python3 online submissions for Max Consecutive Ones.
Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.
Given N, calculate F(N).
Example 1:
Input: 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Note:
0 ≤ N ≤ 30.
C语言
int fib(int N){
if(N==0)return 0;
if(N==1)return 1;
else return fib(N-1)+fib(N-2);
}
Success
Details
Runtime: 12 ms, faster than 41.26% of C online submissions for Fibonacci Number.
Memory Usage: 6.8 MB, less than 40.51% of C online submissions for Fibonacci Number.
python3
class Solution:
def fib(self, N: int) -> int:
a,b = 0,1
for _ in range(N):
a, b = b, a+b
return a
Success
Details
Runtime: 36 ms, faster than 85.00% of Python3 online submissions for Fibonacci Number.
Memory Usage: 13.2 MB, less than 5.02% of Python3 online submissions for Fibonacci Number.