From 2d266b576653720ff16b0a3dfa635b72f5ffa57d Mon Sep 17 00:00:00 2001 From: liao junwu Date: 2024年11月14日 23:24:17 +0800 Subject: [PATCH 1/4] [0739 temperature] add C version add C version for 0739: temperature Signed-off-by: liao junwu --- ...17346円227円245円346円270円251円345円272円246円.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" index 45af52868f..dd633aed9a 100644 --- "a/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" +++ "b/problems/0739.346円257円217円346円227円245円346円270円251円345円272円246円.md" @@ -215,6 +215,38 @@ public: ## 其他语言版本 +### C: + +```C +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize) { + int len = temperaturesSize; + *returnSize = len; + + int *result = (int *)malloc(sizeof(int) * len); + memset(result, 0x00, sizeof(int) * len); + + int stack[len]; + memset(stack, 0x00, sizeof(stack)); + int top = 0; + + for (int i = 1; i < len; i++) { + if (temperatures[i] <= temperatures[stack[top]]) { /* push */ + stack[++top] = i; + } else { + while (top>= 0 && temperatures[i]> temperatures[stack[top]]) { /* stack not empty */ + result[stack[top]] = i - stack[top]; + top--; /* pop */ + } + stack[++top] = i; /* push */ + } + } + return result; +} +``` + ### Java: ```java From da88f5e4c1407d99b46f087a6d5d10f6e685844e Mon Sep 17 00:00:00 2001 From: liao junwu Date: 2024年11月16日 15:42:44 +0800 Subject: [PATCH 2/4] [0496 the next bigger element] add C version add C version for 0496: the next bigger element Signed-off-by: liao junwu --- ...4345円244円247円345円205円203円347円264円240円I.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" index 54182d3018..02e73a588d 100644 --- "a/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" +++ "b/problems/0496.344円270円213円344円270円200円344円270円252円346円233円264円345円244円247円345円205円203円347円264円240円I.md" @@ -195,6 +195,62 @@ public: 建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简! ## 其他语言版本 + +### C + +``` C +/* 先用单调栈的方法计算出结果,再根据nums1中的元素去查找对应的结果 */ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) { + + /* stcak */ + int top = -1; + int stack_len = nums2Size; + int stack[stack_len]; + //memset(stack, 0x00, sizeof(stack)); + + /* nums2 result */ + int* result_nums2 = (int *)malloc(sizeof(int) * nums2Size); + //memset(result_nums2, 0x00, sizeof(int) * nums2Size); + + /* result */ + int* result = (int *)malloc(sizeof(int) * nums1Size); + //memset(result, 0x00, sizeof(int) * nums1Size); + *returnSize = nums1Size; + + /* init */ + stack[++top] = 0; /* stack loaded with array subscripts */ + + for (int i = 0; i < nums2Size; i++) { + result_nums2[i] = -1; + } + + /* get the result_nums2 */ + for (int i = 1; i < nums2Size; i++) { + if (nums2[i] <= nums2[stack[top]]) { + stack[++top] = i; /* push */ + } else { + while ((top>= 0) && (nums2[i]> nums2[stack[top]])) { + result_nums2[stack[top]] = nums2[i]; + top--; /* pop */ + } + stack[++top] = i; + } + } + + /* get the result */ + for (int i = 0; i < nums1Size; i++) { + for (int j = 0; j < nums2Size; j++) { + if (nums1[i] == nums2[j]) { + result[i] = result_nums2[j]; + } + } + } + return result; +} +``` ### Java ```java From 0fa443ce1a234704898630c188640f4512e9f7d1 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: 2024年12月26日 14:03:38 -0600 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20150=E9=A2=98=E6=9B=B4=E6=AD=A3Python?= =?UTF-8?q?=E8=A7=A3=E6=B3=95=E4=B8=AD=E4=BD=BF=E7=94=A8eval()=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此处原本提供的两个python解法是一样的,并无区别;更正为实际上真正使用eval()的方法。 --- ...76345円274円217円346円261円202円345円200円274円.md" | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" index 7d4031d7da..4ffe950b5b 100644 --- "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" +++ "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" @@ -188,34 +188,20 @@ class Solution(object): return stack.pop() ``` -另一种可行,但因为使用eval相对较慢的方法: +另一种可行,但因为使用eval()相对较慢的方法: ```python -from operator import add, sub, mul - -def div(x, y): - # 使用整数除法的向零取整方式 - return int(x / y) if x * y> 0 else -(abs(x) // abs(y)) - class Solution(object): - op_map = {'+': add, '-': sub, '*': mul, '/': div} - - def evalRPN(self, tokens): - """ - :type tokens: List[str] - :rtype: int - """ + def evalRPN(self, tokens: List[str]) -> int: stack = [] for token in tokens: - if token in self.op_map: - op1 = stack.pop() - op2 = stack.pop() - operation = self.op_map[token] - stack.append(operation(op2, op1)) + # 判断是否为数字,因为isdigit()不识别负数,故需要排除第一位的符号 + if token.isdigit() or (len(token)>1 and token[1].isdigit()): + stack.append(token) else: - stack.append(int(token)) - return stack.pop() - - + op2 = stack.pop() + op1 = stack.pop() + stack.append(str(int(eval(op1 + token + op2)))) + return int(stack.pop()) ``` ### Go: From 0638ba5ad88909b1463ccb43f55cb06f9bc88d09 Mon Sep 17 00:00:00 2001 From: Yuan Yuan Date: 2024年12月26日 14:09:06 -0600 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20150=E9=A2=98=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" index 4ffe950b5b..5fb28c29d4 100644 --- "a/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" +++ "b/problems/0150.351円200円206円346円263円242円345円205円260円350円241円250円350円276円276円345円274円217円346円261円202円345円200円274円.md" @@ -200,6 +200,7 @@ class Solution(object): else: op2 = stack.pop() op1 = stack.pop() + # 由题意"The division always truncates toward zero",所以使用int()可以天然取整 stack.append(str(int(eval(op1 + token + op2)))) return int(stack.pop()) ```

AltStyle によって変換されたページ (->オリジナル) /