From 60f3439c8377ef8b99b5055b471840d53ab200a0 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Sat, 27 Sep 2025 20:37:04 -0600 Subject: 461. hamming distance --- hamming_distance.c | 22 ++++++++++++++++++++++ hamming_distance.py | 27 +++++++++++++++++++++++++++ largest_perimeter_triangle.c | 24 ++++++++++++++++++++++++ largest_perimeter_triangle.py | 26 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 hamming_distance.c create mode 100644 hamming_distance.py create mode 100644 largest_perimeter_triangle.c create mode 100644 largest_perimeter_triangle.py diff --git a/hamming_distance.c b/hamming_distance.c new file mode 100644 index 0000000..625f178 --- /dev/null +++ b/hamming_distance.c @@ -0,0 +1,22 @@ +// 461. Hamming Distance +#include "leetcode.h" + +/* + * the hamming distance between two integers is the number of positions at which + * the corresponding bits are different. given two integers 'x' and 'y', return + * the hamming distance between them. + */ + +int hammingDistance(int x, int y) { + int ans = 0, n = x ^ y; + while (n) { + ++ans; + n &= n - 1; + } + return ans; +} + +int main() { + printf("%d\n", hammingDistance(1, 4)); // expect: 2 + printf("%d\n", hammingDistance(3, 1)); // expect: 1 +} diff --git a/hamming_distance.py b/hamming_distance.py new file mode 100644 index 0000000..715b35a --- /dev/null +++ b/hamming_distance.py @@ -0,0 +1,27 @@ +# 461. Hamming Distance + +""" +the hamming distance between two integers is the number of positions at which +the corresponding bits are different. given two integers 'x' and 'y', return +the hamming distance between them. +""" + + +class Solution(object): + def hammingDistance(self, x, y): + """ + :type x: int + :type y: int + :rtype: int + """ + ans, n = 0, x ^ y + while n: + ans += 1 + n &= n - 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.hammingDistance(1, 4)) + print(obj.hammingDistance(3, 1)) diff --git a/largest_perimeter_triangle.c b/largest_perimeter_triangle.c new file mode 100644 index 0000000..b31fa69 --- /dev/null +++ b/largest_perimeter_triangle.c @@ -0,0 +1,24 @@ +// 976. Largest Perimeter Triangle +#include "leetcode.h" + +/* + * given an integer array 'nums', return the largest perimeter of a triangle + * with a non-zero area, formed from three of these lengths. if it is impossible + * to form any triangle of a non-zero area, return 0. + */ + +int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); } + +int largestPerimeter(int *nums, int numsSize) { + qsort(nums, numsSize, sizeof(int), cmp); + for (int i = numsSize - 3; i >= 0; i--) + if (nums[i] < nums[i - 1] + nums[i - 2]) + return nums[i] + nums[i] + nums[i - 2]; + return 0; +} + +int main() { + int n1[] = {2, 1, 2}, n2[] = {3, 2, 3, 4}; + printf("%d\n", largestPerimeter(n1, ARRAY_SIZE(n1))); // expect: 5 + printf("%d\n", largestPerimeter(n2, ARRAY_SIZE(n2))); // expect: 0 +} diff --git a/largest_perimeter_triangle.py b/largest_perimeter_triangle.py new file mode 100644 index 0000000..4ee442c --- /dev/null +++ b/largest_perimeter_triangle.py @@ -0,0 +1,26 @@ +# 976. Largest Perimeter Triangle + +""" +given an integer array 'nums', return the largest perimeter of a triangle +with a non-zero area, formed from three of these lengths. if it is impossible +to form any triangle of a non-zero area, return 0. +""" + + +class Solution(object): + def largestPerimeter(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + nums = sorted(nums)[::-1] + for i in range(len(nums) - 2): + if nums[i] < nums[i + 1] + nums[i + 2]: + return nums[i] + nums[i + 1] + nums[i + 2] + return 0 + + +if __name__ == "__main__": + obj = Solution() + print(obj.largestPerimeter(nums=[2, 1, 2])) + print(obj.largestPerimeter(nums=[3, 2, 3, 4])) -- cgit v1.2.3