Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43b4bd7

Browse files
committed
Distinct pairs done
1 parent 270ed1b commit 43b4bd7

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.leetcode.math;
2+
3+
/**
4+
* Level: Easy
5+
* Problem Link: https://leetcode.com/problems/excel-sheet-column-number/
6+
* Problem Description:
7+
* Given a column title as appear in an Excel sheet, return its corresponding column number.
8+
*
9+
* For example:
10+
*
11+
* A -> 1
12+
* B -> 2
13+
* C -> 3
14+
* ...
15+
* Z -> 26
16+
* AA -> 27
17+
* AB -> 28
18+
* ...
19+
*
20+
* Example 1:
21+
* Input: "A"
22+
* Output: 1
23+
*
24+
* Example 2:
25+
* Input: "AB"
26+
* Output: 28
27+
*
28+
* Example 3:
29+
* Input: "ZY"
30+
* Output: 701
31+
*
32+
* @author rampatra
33+
* @since 2019年05月31日
34+
*/
35+
public class ExcelSheetColumnNumber {
36+
37+
private static int titleToNumber(String title) {
38+
return 0;
39+
}
40+
41+
public static void main(String[] args) {
42+
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.rampatra.arrays;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Level: Easy
8+
* Problem Description:
9+
* Given an array and a target sum, return the number of distinct pairs whose sum is equal to the target sum.
10+
* <p>
11+
* For Example, given an array [1, 2, 3, 6, 7, 8, 9, 1] and a target sum of 10,
12+
* the 7 pairs, i.e, (1, 9), (2, 8), (3, 7), (8, 2), (9, 1), (9, 1), and (1, 9) all sum to 10 but there are only
13+
* three distinct pairs, i.e, (1, 9), (2, 8), and (3, 7) so the answer would be 3.
14+
*
15+
* @author rampatra
16+
* @since 2019年06月03日
17+
*/
18+
public class DistinctPairs {
19+
20+
/**
21+
* Time complexity: O(n), n = size of the array
22+
* Space complexity: O(n)
23+
*
24+
* @param arr
25+
* @param targetSum
26+
* @return
27+
*/
28+
private static int numberOfDistinctPairs(int[] arr, int targetSum) {
29+
Set<Integer> numSet = new HashSet<>();
30+
Set<Set<Integer>> pairSet = new HashSet<>();
31+
32+
for (int i = 0; i < arr.length; i++) {
33+
if (numSet.contains(targetSum - arr[i])) {
34+
Set<Integer> pair = new HashSet<>();
35+
pair.add(arr[i]);
36+
pair.add(targetSum - arr[i]);
37+
pairSet.add(pair);
38+
}
39+
numSet.add(arr[i]);
40+
}
41+
42+
return pairSet.size();
43+
}
44+
45+
public static void main(String[] args) {
46+
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 1));
47+
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 2));
48+
System.out.println(numberOfDistinctPairs(new int[]{1, 2, 3, 6, 7, 8, 9, 1}, 10));
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /