Skip to content

[pull] master from begeekmyfriend:master #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions 0026_remove_duplicates_from_sorted_array/rm_dup.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#include <stdio.h>
#include <stdlib.h>


static int removeDuplicates(int* nums, int numsSize)
{
if (numsSize <= 1) {
return numsSize;
}

int i, count = 1;
int i, size = 0;
for (i = 1; i < numsSize; i++) {
if (nums[i - 1] != nums[i]) {
nums[count++] = nums[i];
if (nums[size] != nums[i]) {
nums[++size] = nums[i];
}
}

return count;
return size + 1;
}

int main(int argc, char **argv)
Expand Down
13 changes: 5 additions & 8 deletions 0026_remove_duplicates_from_sorted_array/rm_dup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ using namespace std;
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() == 0) {
return 0;
}

int count = 1;
int size = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i - 1] != nums[i]) {
nums[count++] = nums[i];
if (nums[size] != nums[i]) {
nums[++size] = nums[i];
}
}
return count;

return size + 1;
}
};
18 changes: 9 additions & 9 deletions 0070_climbing_stairs/climb_stairs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
#include <stdlib.h>
#include <string.h>

static int dfs(int n, int *count)
static int dfs(int n, int *steps)
{
if (n == 1) {
return 1;
} else if (n == 2) {
return 2;
} else if (count[n] > 0) {
return count[n];
} else if (steps[n] > 0) {
return steps[n];
} else {
count[n] += dfs(n - 1, count);
count[n] += dfs(n - 2, count);
return count[n];
steps[n] += dfs(n - 1, steps);
steps[n] += dfs(n - 2, steps);
return steps[n];
}
}

static int climbStairs(int n)
{
#if 1
if (n < 1) return 0;
int *count = malloc((n + 1) * sizeof(int));
memset(count, 0, (n + 1) * sizeof(int));
return dfs(n, count);
int *steps = malloc((n + 1) * sizeof(int));
memset(steps, 0, (n + 1) * sizeof(int));
return dfs(n, steps);
#else
int i, a = 1, b = 2, c;
for (i = 3; i <= n; i++) {
Expand Down
8 changes: 4 additions & 4 deletions 0076_minimum_window_substring/window_substring.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static char *minWindow(char *s, char *t)
int l = 0, r = 0;
int min_len = slen + 1;
int start = 0;
int chars_to_meet = 0;
int len = 0;

for (i = 0; i < tlen; i++) {
count[t[i]]++;
Expand All @@ -31,18 +31,18 @@ static char *minWindow(char *s, char *t)
while (r < slen) {
if (--count[s[r++]] >= 0) {
/* pattern found */
chars_to_meet++;
len++;
}

while (chars_to_meet == tlen) {
while (len >= tlen) {
if (r - l < min_len) {
min_len = r - l;
start = l;
}

/* Chars with negative count are not included in the pattern string */
if (++count[s[l++]] > 0) {
chars_to_meet--;
len--;
}
}
}
Expand Down
97 changes: 51 additions & 46 deletions 0224_basic_calculator/calculator.c
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

static int calculator(char *s)

static int dfs(char **input)
{
int n;
int pos1 = 0;
int pos2 = 0;
int *nums = malloc(1000 * sizeof(int));
char *signs = malloc(1000 * sizeof(char));
int i, res = 0;
int num = 0;
int stk[700], pos = 0;
char sign = '+';
char *s = *input;

nums[pos1++] = 0;
while (*s != '\0') {
switch (*s) {
case '+':
case '-':
case '(':
signs[pos2++] = *s;
break;
case ')':
--pos2;
if (pos1 >= 2 && pos2 > 0 && signs[pos2 - 1] != '(') {
n = nums[--pos1];
int a = nums[--pos1];
if (signs[--pos2] == '+') {
n = a + n;
} else {
n = a - n;
}
}
nums[pos1++] = n;
break;
case ' ':
break;
default:
n = 0;
while(*s >= '0' && *s <= '9') {
n = n * 10 + (*s - '0');
s++;
}
s--;
char c = *s++;
if (isdigit(c)) {
num = 10 * num + (c - '0');
}

if (c == '(') {
/* dfs("2*(1+3)") = 2 * dfs("1+3") */
num = dfs(&s);
}

if (pos1 >= 2 && signs[pos2 - 1] != '(' && signs[pos2 - 1] != '(') {
int a = nums[--pos1];
if (signs[--pos2] == '+') {
n = a + n;
} else {
n = a - n;
}
if (!isdigit(c) && c != ' ' || *s == '\0') {
switch (sign) {
case '+':
stk[pos++] = num;
break;
case '-':
stk[pos++] = -num;
break;
case '*':
stk[pos - 1] *= num;
break;
case '/':
stk[pos - 1] /= num;
break;
}
nums[pos1++] = n;
break;
/* update the sign and reset the number */
sign = c;
num = 0;
}
s++;

/* return from the dfs */
if (c == ')')
break;
}

/* update position */
*input = s;

while (pos > 0) {
res += stk[--pos];
}

return n;
return res;
}

static int calculator(char *s)
{
return dfs(&s);
}

int main(int argc, char **argv)
Expand Down
28 changes: 28 additions & 0 deletions 0560_subarray_sum_equals_k/subarray_sum.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = 0, sum = 0;
unordered_map<int, int> pre_sum_cnt;

// The prefix sum array records the sum of nums[0...i], so we have
// presum[j] - presum[j] = k when the sum of nums[i...j] equals k.
// The presum[0] should always be 0. And pre_sum_cnt[0] = 1.
pre_sum_cnt[0] = 1;
for (const auto n : nums) {
// Here the sum means sum of nums[0...j] and the sum0 means sum
// of nums[0...i] then there will be sum - sum0 = k.
sum += n;
int sum0 = sum - k;
if (ht.count(sum0)) {
res += pre_sum_cnt[sum0];
}
pre_sum_cnt[sum]++;
}

return res;
}
};