From ed74dfd2fef09e0ff0c8cc6b408bbcedeee7575a Mon Sep 17 00:00:00 2001 From: Giggle Ding Date: Mon, 8 Jan 2024 06:42:47 +0800 Subject: [PATCH] feat: add solutions to lcof2 problem: No.012 --- .../README.md" | 23 +++++++++++++++++++ .../Solution.c" | 18 +++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" index 61a7f927c2a86..1aaad5f77f5cc 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" @@ -198,6 +198,29 @@ class Solution { } ``` +### **C** + +```c +int pivotIndex(int* nums, int numsSize) { + int left, right; + left = 0; + right = 0; + + for (int i = 0; i < numsSize; i++) { + right += nums[i]; + } + + for (int i = 0; i < numsSize; i++) { + right -= nums[i]; + if (right == left) + return i; + left += nums[i]; + } + + return -1; +} +``` + ### **...** ``` diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" new file mode 100644 index 0000000000000..b655283a1d610 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.c" @@ -0,0 +1,18 @@ +int pivotIndex(int* nums, int numsSize) { + int left, right; + left = 0; + right = 0; + + for (int i = 0; i < numsSize; i++) { + right += nums[i]; + } + + for (int i = 0; i < numsSize; i++) { + right -= nums[i]; + if (right == left) + return i; + left += nums[i]; + } + + return -1; +} \ No newline at end of file