1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Subscribe to see which companies asked this question.
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int i,j,sum;
int *res = (int *) malloc(sizeof(int)*2);
for(i=0;i<numsSize;i++){
res[0] = i;
for(j=i+1;j<numsSize;j++){
if(nums[i] +nums[j] == target){
res[1] = j;
return res;
}
}
}
return res;
}
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Subscribe to see which companies asked this question.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int a=0,b=0,i = 0;
struct ListNode *ln = (struct ListNode*)malloc(sizeof(struct ListNode));
ln->val = 0;
ln->next = NULL;
struct ListNode *p = ln;
struct ListNode *p1 = l1;
struct ListNode *p2 = l2;
while(p1!=NULL||p2!=NULL){
struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));
l->val=0;
l->next = NULL;
p->next = l;
p = l;
if(p1)a=p1->val;else a=0;
if(p2)b=p2->val;else b=0;
p->val = a+b+i;
if(p->val/10>=1){
i = 1;
p->val = p->val%10;
}else{
i = 0;
}
if(p1)p1=p1->next;
if(p2)p2=p2->next;
}
if(i==1){
struct ListNode *l = (struct ListNode*)malloc(sizeof(struct ListNode));
l->val=1;
l->next = NULL;
p->next = l;
p = l;
}
return ln->next;
}
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
Subscribe to see which companies asked this question.
int findstr(char *s,int len,char a){
int i;
for(i=0;i<len;i++){
if(s[i]==a)return 0;
}
return 1;
}
int lengthOfLongestSubstring(char* s) {
int i=0,m=0;
char a;
while(*s!='\0'){
a = s[i];
while(a!='\0')a=findstr(s,i,a)?s[++i]:'\0';
if(i>m)m=i;
s++;i=1;
}
return m;
}
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3] nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
Subscribe to see which companies asked this question.
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
double r = 0;
int a=0,b=0,i=0,j=0,x=0;
int *n = (int*)malloc(sizeof(int)*(nums1Size+nums2Size));
memset(n,9,nums1Size+nums2Size);
while(x<nums1Size+nums2Size){
if(i<nums1Size){
a = nums1[i];
}else{
n[x] = nums2[j];
j++;x++;continue;
}
if(j<nums2Size){
b=nums2[j];
}else{
n[x] = nums1[i];
i++;x++;continue;
}
a<b?(i++,n[x]=a):(j++,n[x]=b);
x++;
}
if(x%2!=1)
r = (n[x/2]+n[x/2-1])/2.0;
else
r = n[x/2];
return r;
}