prompts
stringclasses 10
values | references
stringclasses 10
values |
|---|---|
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
|
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
int i, j;
int *ret = calloc(2, sizeof(int));
for (i = 0; i < numsSize; i++)
{
int key = target - nums[i];
for (j = i + 1; j < numsSize; j++)
if (nums[j] == key)
{
ret[0] = i;
ret[1] = j;
}
}
*returnSize = 2;
return ret;
}
|
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 contains a single digit. Add the two numbers and return the sum as a linked list.
|
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
struct ListNode *head = NULL;
struct ListNode *walk = NULL;
struct ListNode *tmp = NULL;
int carry = 0;
int val1 = 0;
int val2 = 0;
int val = 0;
while (l1 != NULL || l2 != NULL || carry)
{
val1 = 0;
val2 = 0;
val = 0;
if (l1)
{
val1 = l1->val;
l1 = l1->next;
}
if (l2)
{
val2 = l2->val;
l2 = l2->next;
}
val = carry + val1 + val2;
carry = val / 10;
tmp = malloc(sizeof(struct ListNode));
tmp->val = val % 10;
tmp->next = NULL;
if (!head)
{
head = walk = tmp;
}
else
{
walk->next = tmp;
walk = walk->next;
}
}
return head;
}
|
Given a string s, find the length of the longest substring without repeating characters. Do not use brute force.
|
int lengthOfLongestSubstring(char *str)
{
int n = strlen(str);
if (!n)
return 0;
int L_len = 1;
int C_len = 1;
int P_ind, i;
int visited[256];
memset(visited, -1, sizeof(int) * 256);
visited[str[0]] =
0;
for (i = 1; i < n; i++)
{
P_ind = visited[str[i]];
if (P_ind == -1 || i - C_len > P_ind)
C_len++;
else
{
if (C_len > L_len)
L_len = C_len;
C_len = i - P_ind;
}
visited[str[i]] = i;
}
if (C_len > L_len)
L_len = C_len;
return L_len;
}
|
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
|
double findMedianSortedArrays(int *nums1, int nums1Size, int *nums2, int nums2Size)
{
int index1 = 0;
int index2 = 0;
int v[nums1Size + nums2Size];
int v_index = 0;
while (index1 < nums1Size && index2 < nums2Size)
{
if (nums1[index1] <= nums2[index2])
{
v[v_index++] = nums1[index1++];
}
else
{
v[v_index++] = nums2[index2++];
}
}
if (index1 < nums1Size)
{
while (index1 < nums1Size)
{
v[v_index++] = nums1[index1++];
}
}
if (index2 < nums2Size)
{
while (index2 < nums2Size)
{
v[v_index++] = nums2[index2++];
}
}
if (v_index == 1)
{
return v[0];
}
if (v_index % 2 == 0)
{
double n1, n2;
n1 = v[v_index / 2];
n2 = v[(v_index / 2) - 1];
return (n1 + n2) / 2;
}
int new_index = (int)v_index / 2;
int i = 0;
return v[new_index];
}
|
Given a string s, return the longest palindromic substring in s.
|
#include <stdlib.h>
#include <string.h>
char * longestPalindrome(char * s) {
int si_max = 0, ei_max = 0, sz_max = 0, sz, i, delta_i;
char ch, *s_longest;
if (s[1] == '') return s;
for (ch = s[1], i = 1; ch != ''; ch = s[++i]) {
if (s[i - 1] == ch) {
sz = 2;
delta_i = 1;
while (i - 1 - delta_i >= 0 && s[i + delta_i] != '' && s[i - 1 - delta_i] == s[i + delta_i]) {
sz += 2;
delta_i += 1;
}
if (sz > sz_max) {
sz_max = sz;
si_max = i - 1 - delta_i + 1;
ei_max = i + delta_i - 1;
}
}
}
for (ch = s[0], i = 1; ch != ''; ch = s[++i]) {
sz = 1;
delta_i = 1;
while (i - delta_i >= 0 && s[i + delta_i] != '' && s[i - delta_i] == s[i + delta_i]) {
sz += 2;
delta_i += 1;
}
if (sz > sz_max) {
sz_max = sz;
si_max = i - delta_i + 1;
ei_max = i + delta_i - 1;
}
}
if ((s_longest = (char *) malloc(sizeof(s))) == NULL) {
return NULL;
}
strncpy(s_longest, s + si_max, sz_max);
s_longest[sz_max] = '';
return s_longest;
}
|
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0.
|
#include <limits.h>
int reverse(int x)
{
int rev = 0;
while (x != 0)
{
int pop = x % 10;
x /= 10;
if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7))
return 0;
if (rev < INT_MIN / 10 || (rev == INT_MIN / 10 && pop < -8))
return 0;
rev = rev * 10 + pop;
}
return rev;
}
|
Given an integer x, return true if x is a palindrome, and false otherwise.
|
bool isPalindrome(int x)
{
if (x < 0 || (x % 10 == 0 && x != 0))
{
return false;
}
int revertedNumber = 0;
while (x > revertedNumber)
{
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
return x == revertedNumber || x == revertedNumber / 10;
}
|
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where '.' matches any single character and '*' matches zero or more of the preceding element. The matching should cover the entire input string.
|
bool matchStar(char ch, char* s, char* p) {
do {
if (isMatch(s, p))
return true;
} while (*s != '' && (*s++ == ch || ch == '.'));
return false;
}
bool isMatch(char* s, char* p) {
if (*p == '')
return *s == '';
if (p[1] == '*')
return matchStar(p[0], s, p + 2);
if (*s != '' && (p[0] == '.' || *p == *s)) {
return isMatch(s + 1, p + 1);
}
return false;
}
|
Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string.
|
int findMaxConsecutiveOnes(int* nums, int numsSize){
int i=0;
int maxCount=0;
int count = 0;
while(i<numsSize){
while(i<numsSize && nums[i]!=0){
count++;
i++;
}
if(maxCount<=count){
maxCount = count;
}
count = 0;
while(i<numsSize && nums[i]==0){
i++;
}
}
return maxCount;
}
|
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
|
#include <stdlib.h>
int cmp(const void* a, const void* b) {
const int *A = a, *B = b;
return (*A > *B) - (*A < *B);
}
int threeSumClosest(int* nums, int nums_size, int target) {
int i, j, k, result, sum3;
qsort(nums, nums_size, sizeof(int), cmp);
result = nums[0] + nums[1] + nums[2];
for (i = 0; i < nums_size - 2; i++) {
j = i + 1;
k = nums_size - 1;
while (j < k) {
sum3 = nums[i] + nums[j] + nums[k];
if (abs(target - sum3) < abs(target - result)) {
result = sum3;
}
if (sum3 < target) {
j++;
} else if (sum3 > target) {
k--;
} else {
return sum3;
}
}
}
return result;
}
|
README.md exists but content is empty.
- Downloads last month
- 1