Leetcode18 [LeetCode] 2283. Check if Number Has Equal Digit Count and Digit Value, C Solution - Use Hash Table Time Complexity O(n) Space Complexity O(n) #include bool digitCount(char* pnum); int main() { char string[] = "030"; bool RetVal; RetVal = digitCount(string); return 0; } bool digitCount(char* pnum) { bool bRetVal = true; const int ASCIINumberInit = 48; int i = 0; int NumOfChar = 0; int arrHashTable[10] = { 0 }; while (pnum[NumOfChar] != '\0') { arrHashTable[pnum[NumOfC.. 2023. 1. 1. [LeetCode] 888. Fair Candy Swap, C Solution Get Total Sum of alice and bob candis number Calculate how many candies should be exchanged, to get equal num each other. But this method Runtime too much, only Beats 7.14% in Leet Code. Topics (presented in LeetCode) Hash Table Binary Search Sorting int* fairCandySwap( int* aliceSizes, int aliceSizesSize, int* bobSizes, int bobSizesSize, int* returnSize) { int i = 0; int aliceCandyNum .. 2022. 12. 31. [LeetCode] 1941. Check if All Characters Have Equal Number of Occurrences, C Solution - Use Hash Map - Compare Counting Number of each Character (except 0 occurence character) #define NumOfLowerCaseEnglishLetter 26 #define ASCII_INIT_NUM_IDX 97 bool areOccurrencesEqual(char* s) { long int NumOfChar = 0; int RefNum = 0; int arr[NumOfLowerCaseEnglishLetter] = { 0 }; // consists of lowercase English letters bool RetVal = true; while (s[NumOfChar] != '\0') { ++arr[s[NumOfCha.. 2022. 12. 31. [LeetCode] 383. Ransom Note, C Solution 1 using Hash Map Intuition Declare Hash Map represents charactoer 'a' to 'z' (size 26) Increase Hash Map Index which is in Magazine Note Decrease Hash Map Index which is in ransom Note If one of value in Hash Map under 0, Return false (it means character of Magazine Note doesn't represent ransom Note) When you use index, use should consider ASCII number of 'a' at inital index (97) bool .. 2022. 12. 30. [LeetCode] 876. Middle of the Linked List, C Idea When we need to find median value, index -> we can use this algorithms move two step forward agent, move onstep forward agent per scenario. /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* middleNode(struct ListNode* head){ struct ListNode* first = head; struct ListNode* second = head; while(first && first->next) { sec.. 2022. 12. 28. [LeetCode] 1342 Number of Steps to Reduce a Number to Zero, C Solution Simple sequential calculation Other Solution can use binary operation int numberOfSteps(int num) { int step = 0; while (num != 0) { if (num % 2 == 0) { num = num / 2; } else { num -= 1; } ++step; } return step; } 2022. 12. 28. 이전 1 2 3 다음