栈实现队列
使用两个栈,一个栈用于接受输入(push),一个栈用于输出(pop和peek)。
public class QueueByStack {
Stack<Integer> inStack;
Stack<Integer> outStack;
public QueueByStack(){
inStack = new Stack<>();
outStack = new Stack<>();
}
public void push(int x){
inStack.push(x);
}
public int pop(){
if (outStack.isEmpty()){
in2out();
}
return outStack.pop();
}
public int peek(){
if (outStack.isEmpty()){
in2out();
}
return outStack.peek();
}
public boolean isEmpty(){
return outStack.isEmpty()&&inStack.isEmpty();
}
public void in2out(){
while (!inStack.isEmpty()){
outStack.push(inStack.pop());
}
}
}
队列实现栈
使用两个队列queue1和queue2实现栈。在进行push操作时,先将输入值压入队列queue2中,再将queue1的所有元素压入queue2中(当queue1不为空时),然后将queue2和queue1交换,使得后来的元素在队列queue1的第一个元素。
public class StackByQueue {
Queue<Integer> queue1;
Queue<Integer> queue2;
public StackByQueue(){
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 isEmpty(){
return queue1.isEmpty();
}
}
n数之和
2数之和
给定一个数组nums和一个整数target,要求返回和为target的两个数组元素的下标。
双重for循环解决
第一层for循环确定一个数nums[i],第二层for循环确定另一个数nums[j]。
public int[] twoSum(int[] nums,int target){
int n = nums.length;
for (int i = 0;i < n; i ++){
for (int j = i + 1;j < n;j++){
if (nums[i]+nums[j]==target){
return new int[]{i,j};
}
}
}
return new int[0];
}
使用hash对时间复杂度进行优化,对数组nums进行遍历,如果hash表中的key不和target-nums[i]匹配,就将nums[i],i放入hash表,以保证不和自己匹配。
public int[] twoSumByHash(int[] nums, int target){
Map<Integer,Integer> hashtable = new HashMap<>();
int n = nums.length;
for (int i = 0;i<n;i++){
if (hashtable.containsKey(target-nums[i])){
return new int[]{hashtable.get(target-nums[i]),i};
}
hashtable.put(nums[i],i);
}
return new int[0];
}
3数之和
找出nums数组中和为0的三个元素的值。
直接3层for循环时间复杂度为O(),太高。先对数组排序,遇到相同的值就跳过,使得时间复杂度降低。进行遍历,先确定一个数nums[first],如果nums[first]==nums[first-1]&&first>0,说明值相同,使用continue遍历下一个值;令third=nums.length-1,target=-nums[first],对first后元素进行遍历,令second=first+1,如果nums[second]==nums[second-1]&&second<third,说明值相同,使用continue遍历下一个值;当second<third&&nums[third]+nums[second]>target时,third--,如果second==third,退出second的循环,如果nums[second]+nums[third]==target,找到一组答案。
public List<List<Integer>> threeSum(int[] nums){
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
for (int first = 0;first<n;++first){
if (first > 0 && nums[first] == nums[first-1]) {
continue;
}
int third= n-1;
int target = -nums[first];
for (int second = first+1;second<n;++second){
if (second>first+1&&nums[second]==nums[second-1]){
continue;
}
//因为数组升序排列,所以对nums[second]+nums[third]>target进行判断
while (second < third&&nums[second]+nums[third]>target){
--third;
}
if (second==third){
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;
}