leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 60f3439c8377ef8b99b5055b471840d53ab200a0
parent 48f4961febc7d03056cd826682e4b3277c7b20ca
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sat, 27 Sep 2025 20:37:04 -0600

461. hamming distance

Diffstat:
Ahamming_distance.c | 22++++++++++++++++++++++
Ahamming_distance.py | 27+++++++++++++++++++++++++++
Alargest_perimeter_triangle.c | 24++++++++++++++++++++++++
Alargest_perimeter_triangle.py | 26++++++++++++++++++++++++++
4 files changed, 99 insertions(+), 0 deletions(-)

diff --git a/hamming_distance.c 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 @@ -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 @@ -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 @@ -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]))