aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hamming_distance.c22
-rw-r--r--hamming_distance.py27
-rw-r--r--largest_perimeter_triangle.c24
-rw-r--r--largest_perimeter_triangle.py26
4 files changed, 99 insertions, 0 deletions
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]))