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 d34d081

Browse files
author
C5141506
committed
leetcode problem
1 parent f421676 commit d34d081

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package java_problem.array;
2+
3+
import java.util.Arrays;
4+
5+
public class MoveZeroes {
6+
public static void main(String[] args){
7+
moveZeroes(new int[]{1,0,2,0,3,4});
8+
}
9+
10+
public static void moveZeroes(int[] nums) {
11+
Arrays.sort(nums);
12+
if(nums.length>1){
13+
int zeroCount=0;
14+
for(int value:nums){
15+
if(value!=0){
16+
break;
17+
}
18+
zeroCount++;
19+
}
20+
if(zeroCount>0){
21+
int k=0;
22+
int i=zeroCount;
23+
while(i<nums.length){
24+
nums[k]=nums[i];
25+
k++;
26+
i++;
27+
}
28+
while(zeroCount>0){
29+
nums[k]=0;
30+
k++;
31+
zeroCount--;
32+
}
33+
}
34+
}
35+
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package java_problem.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class GenerateParentheses {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.generateParenthesis(3));
10+
}
11+
12+
}
13+
14+
class Solution {
15+
private final List<String> ls = new ArrayList<>();
16+
private final StringBuilder sb = new StringBuilder();
17+
18+
public List<String> generateParenthesis(int n) {
19+
getGenerateString(n, 1, 1);
20+
return ls;
21+
}
22+
23+
public void getGenerateString(int n, int open, int close) {
24+
if (sb.length() == 2 * n) {
25+
ls.add(sb.toString());
26+
return;
27+
}
28+
29+
if (open <= n) {
30+
sb.append("(");
31+
getGenerateString(n, open + 1, close);
32+
sb.deleteCharAt(sb.length() - 1);
33+
}
34+
35+
if (close < open) {
36+
sb.append(")");
37+
getGenerateString(n, open, close + 1);
38+
sb.deleteCharAt(sb.length() - 1);
39+
return;
40+
}
41+
42+
}
43+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//import java.util.*;
2+
//
3+
//public class Twitter {
4+
// Map<Integer, HashSet<Integer>> followerMap;
5+
// Map<Integer, HashSet<Integer>> tweetsMap;
6+
//
7+
// public Twitter() {
8+
// followerMap = new HashMap<>();
9+
// tweetsMap = new HashMap<>();
10+
// }
11+
//
12+
// public void postTweet(int userId, int tweetId) {
13+
// HashSet<Integer> ls;
14+
// if (tweetsMap.containsKey(userId)) {
15+
// ls = tweetsMap.get(userId);
16+
// } else {
17+
// ls = new HashSet<Integer>();
18+
// }
19+
// ls.add(tweetId);
20+
// tweetsMap.put(userId, ls);
21+
// }
22+
//
23+
// public List<Integer> getNewsFeed(int userId) {
24+
// PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
25+
// HashSet<Integer> tweets = new HashSet<Integer>();
26+
// tweets = tweetsMap.get(userId);
27+
// HashSet<Integer> followerList = followerMap.get(userId);
28+
// if(followerList!=null){
29+
// for (int followerId : followerList) {
30+
// tweets.addAll(tweetsMap.get(followerId));
31+
// }
32+
// }
33+
// if(followerList!=null){
34+
// pq.addAll(tweets);
35+
// tweets.clear();
36+
// int i = 10;
37+
// int size=pq.size();
38+
// while (i > 0 && size!=0) {
39+
// tweets.add(pq.poll());
40+
// i--;
41+
// size--;
42+
// }
43+
// }
44+
//
45+
//
46+
// return new ArrayList<>(tweets);
47+
// }
48+
//
49+
// public void follow(int followerId, int followeeId) {
50+
// if (!followerMap.containsKey(followerId)) {
51+
// HashSet<Integer> ls = new HashSet<Integer>();
52+
// ls.add(followeeId);
53+
// followerMap.put(followerId, ls);
54+
// }
55+
//
56+
//
57+
// }
58+
//
59+
// public void unfollow(int followerId, int followeeId) {
60+
// if (followerMap.containsKey(followerId)) {
61+
// HashSet<Integer> ls = followerMap.get(followerId);
62+
// ls.remove(followeeId);
63+
// followerMap.put(followerId, ls);
64+
//
65+
// }
66+
//
67+
// }
68+
//}
69+
//
70+
//class MaxTime {
71+
// long tweetTime;
72+
// int tweetId;
73+
//
74+
// public MaxTime(long tweetTime,int tweetId) {
75+
// this.tweetTime = tweetTime;
76+
// this.tweetId = tweetId;
77+
// }
78+
//}
79+
//
80+
//class CompareMaxTime implements Comparator<MaxTime> {
81+
//
82+
// @Override
83+
// public int compare(MaxTime o1, MaxTime o2) {
84+
// return Long.compare(o1.tweetTime, o2.tweetTime);
85+
// }
86+
//}
87+
//
88+
//
89+
///**
90+
// * Your Twitter object will be instantiated and called as such:
91+
// * Twitter obj = new Twitter();
92+
// * obj.postTweet(userId,tweetId);
93+
// * List<Integer> param_2 = obj.getNewsFeed(userId);
94+
// * obj.follow(followerId,followeeId);
95+
// * obj.unfollow(followerId,followeeId);
96+
// */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java_problem.heap;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Subsets {
7+
public static void main(String[] args){
8+
System.out.println(subsets(new int[]{1,2,3}));
9+
10+
}
11+
static void subsequence(int i, int[] arr, List<Integer> lst, List<List<Integer>> res){
12+
if(res.contains(lst)) return;
13+
if(i==arr.length){
14+
res.add(new ArrayList<>(lst));
15+
return;
16+
}
17+
lst.add(arr[i]);
18+
subsequence(i+1, arr, lst, res);
19+
lst.remove(lst.size()-1);
20+
subsequence(i+1, arr, lst, res);
21+
}
22+
public static List<List<Integer>> subsets(int[] nums) {
23+
List<List<Integer>> res=new ArrayList<>();
24+
subsequence(0, nums, new ArrayList<>(), res);
25+
return res;
26+
}
27+
}

0 commit comments

Comments
(0)

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