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 958f350

Browse files
added leetcode questions
1 parent 12cc55a commit 958f350

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

‎.idea/runConfigurations.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Leetcode/Leetcode_169.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package Leetcode;
2+
3+
import java.util.HashMap;
4+
// majority element
5+
public class Leetcode_169 {
6+
public static int majorityElement(int[] nums) {
7+
HashMap<Integer, Integer> h = new HashMap<>();
8+
for (int num : nums)
9+
h.put(num, h.getOrDefault(num, 0) + 1);
10+
11+
for (int num : nums)
12+
if (h.get(num) > (nums.length / 2))
13+
return num;
14+
return -1;
15+
}
16+
17+
public static void main(String[] args) {
18+
System.out.println(majorityElement(new int[]{2,2,1,1,1,2,2}));
19+
}
20+
}

‎Leetcode/Leetcode_26.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Leetcode;
2+
// remove duplicates from a sorted array
3+
4+
public class Leetcode_26 {
5+
public static int removeDuplicates(int[] nums) {
6+
StringBuilder a = new StringBuilder();
7+
for (int num : nums) {
8+
if (!a.toString().contains(num + "")) {
9+
a.append(num);
10+
}
11+
}
12+
for (int i = 0; i < a.length(); i++) {
13+
nums[i] = Integer.parseInt(a.charAt(i) + "");
14+
}
15+
return a.length();
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(removeDuplicates(new int[]{0,0,1,1,1,2,2,3,3,4}));
20+
}
21+
}

‎Leetcode/Leetcode_260.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package Leetcode;
2+
3+
import java.util.Arrays;
4+
5+
// single number 3
6+
public class Leetcode_260 {
7+
public int[] singleNumber(int[] nums) {
8+
int[] res = new int[2];
9+
return res;
10+
}
11+
}

‎Leetcode/Leetcode_350.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Leetcode;
2+
3+
/*
4+
* Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many
5+
* times as it shows in both arrays and you may return the result in any order.
6+
* */
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public class Leetcode_350 {
13+
public static int[] intersect(int[] nums1, int[] nums2) {
14+
List<Integer> r = new ArrayList<>();
15+
Arrays.sort(nums1);
16+
Arrays.sort(nums2);
17+
int i = 0, j = 0;
18+
while (i < nums1.length && j < nums2.length) {
19+
if (nums1[i] > nums2[j])
20+
j ++;
21+
else if (nums1[i] < nums2[j])
22+
i ++;
23+
else {
24+
r.add(nums1[i]);
25+
i ++;
26+
j ++;
27+
}
28+
}
29+
int[] res = new int[r.size()];
30+
for (int k = 0;k < r.size();k++) {
31+
res[k] = r.get(k);
32+
}
33+
return res;
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(Arrays.toString(intersect(new int[]{4,9,5}, new int[]{9,4,9,8,4})));
38+
}
39+
}

0 commit comments

Comments
(0)

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