赞
踩
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
If multiple answers are possible, return any of them.
It is guaranteed that the length of the answer string is less than 104 for all the given inputs.
Input: numerator = 1, denominator = 2
Output: “0.5”
Input: numerator = 2, denominator = 1
Output: “2”
Input: numerator = 4, denominator = 333
Output: “0.(012)”
From: LeetCode
Link: 166. Fraction to Recurring Decimal
typedef struct { long long remainder; int position; struct ListNode* next; } ListNode; // Function to create a new node ListNode* createNode(long long remainder, int position) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); newNode->remainder = remainder; newNode->position = position; newNode->next = NULL; return newNode; } // Function to find a node with a given remainder int findPosition(ListNode* head, long long remainder) { ListNode* current = head; while (current) { if (current->remainder == remainder) { return current->position; } current = current->next; } return -1; } // Function to add a node to the linked list void addNode(ListNode** head, long long remainder, int position) { ListNode* newNode = createNode(remainder, position); newNode->next = *head; *head = newNode; } // Function to free the linked list void freeList(ListNode* head) { while (head) { ListNode* temp = head; head = head->next; free(temp); } } // Function to return the fraction in string format char* fractionToDecimal(int numerator, int denominator) { if (numerator == 0) { char* result = (char*)malloc(2 * sizeof(char)); strcpy(result, "0"); return result; } // Allocate an initial result buffer with a reasonable size int initialSize = 64; int currentSize = initialSize; char* result = (char*)malloc(currentSize * sizeof(char)); if (!result) { fprintf(stderr, "Memory allocation failed\n"); exit(1); } memset(result, 0, currentSize); int resultIndex = 0; // Handle negative numbers if ((numerator < 0) ^ (denominator < 0)) { result[resultIndex++] = '-'; } long long num = llabs((long long)numerator); long long den = llabs((long long)denominator); // Append integer part int intPartLength = snprintf(result + resultIndex, currentSize - resultIndex, "%lld", num / den); resultIndex += intPartLength; long long remainder = num % den; if (remainder == 0) { return result; } // Append the decimal point result[resultIndex++] = '.'; // Create a linked list to track remainders ListNode* remainderMap = NULL; while (remainder != 0) { // Check if the result buffer needs to be enlarged if (resultIndex + 2 >= currentSize) { currentSize *= 2; result = (char*)realloc(result, currentSize * sizeof(char)); if (!result) { fprintf(stderr, "Memory reallocation failed\n"); exit(1); } } int existingPos = findPosition(remainderMap, remainder); if (existingPos != -1) { // Insert parentheses int len = strlen(result); for (int i = len; i >= existingPos; --i) { result[i + 1] = result[i]; } result[existingPos] = '('; result[len + 1] = ')'; result[len + 2] = '\0'; freeList(remainderMap); return result; } addNode(&remainderMap, remainder, resultIndex); remainder *= 10; int digitLength = snprintf(result + resultIndex, currentSize - resultIndex, "%lld", remainder / den); resultIndex += digitLength; remainder %= den; } freeList(remainderMap); return result; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。