Leetcode232用栈实现队列
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
public void push(int x) {
queue2.offer(x);
while (!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
Leetcode225用队列实现栈
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
public void push(int x) {
queue2.offer(x);
while (!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
N数之和
Leetcode1两数之和
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i=0;i<nums.length;i++){
if (map.containsKey(target-nums[i])){
return new int[]{map.get(target-nums[i]),i};
}
map.put(nums[i],i);
}
return new int[0];
}
Leetcode15三数之和
public static List<List<Integer>> threeSum(int[] nums) {
int n=nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();//结果
for (int first=0;first<n;first++){
//找到与上一个枚举不同的第一个数
if (first>0&&nums[first]==nums[first-1]){
continue;
}
//2+3==target
int target=-nums[first];
//从后向前的对撞指针
int third=n-1;
//从前向后的对撞指针
for (int second=first+1;second<n;second++){
//在同一个first循环中防止枚举相同的second数
if (second>first+1&&nums[second]==nums[second-1]){
continue;
}
//找到2+3<=target
while (second<third&&nums[second]+nums[third]>target){
third--;
}
//如果找到两个指针对撞才退出循环,说明2+3>target,那么后续循环2会继续增加,2+3一定>现在的2+3没必要继续
if (third==second){
break;
}
//存答案
if (nums[second]+nums[third]==target){
List<Integer> list=new ArrayList<>();
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
return ans;
}
Leetcode18四数之和
public static List<List<Integer>> fourSum(int[] nums, int target) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
for (int fourth = 0; fourth < n-3; fourth++) {
if (fourth > 0 && nums[fourth] == nums[fourth- 1]) {
continue;
}
if ((long) nums[fourth] + nums[fourth + 1] + nums[fourth + 2] + nums[fourth + 3] > target) {
break;
}
if ((long) nums[fourth] + nums[n-3] + nums[n-2] + nums[n-1] < target) {
continue;
}
int target1 = target - nums[fourth];
for (int first = fourth + 1; first < n; first++) {
//找到与上一个枚举不同的第一个数
if (first > fourth + 1 && nums[first] == nums[first - 1]) {
continue;
}
//2+3==target
int target2 = target1 - nums[first];
//从后向前的对撞指针
int third = n - 1;
//从前向后的对撞指针
for (int second = first + 1; second < n; second++) {
//在同一个first循环中防止枚举相同的second数
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
//找到2+3<=target
while (second < third && nums[second] + nums[third] > target2) {
third--;
}
//如果找到两个指针对撞才退出循环,说明2+3>target,那么后续循环2会继续增加,2+3一定>现在的2+3没必要继续
if (third == second) {
break;
}
//存答案
if (nums[second] + nums[third] == target2) {
List<Integer> list = new ArrayList<>();
list.add(nums[fourth]);
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
}
return ans;
}