1 调整数组顺序使奇数位于偶数前面(保证相对位置不变)
时间复杂度O(n) 空间复杂度O(n)
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
public int[] reOrderArray (int[] array) {
// write code here
Queue<Integer> queue = new LinkedList<>();
int left=0;
for(int i=0;i<array.length;i++){
if(array[i]%2==0){
queue.offer(array[i]);
}else{
array[left++] = array[i];
}
}
while(!queue.isEmpty()){
array[left++] = queue.poll();
}
return array;
}
}
**
1.1 调整数组顺序使奇数位于偶数前面(相对位置可以改变)
**
class Solution {
public int[] exchange(int[] nums) {
int left = 0;
int right = nums.length-1;
while(left<=right){
while(left<=right&&nums[left]%2!=0){
left++;
}
while(left<=right&&nums[right]%2==0){
right--;
}
if(left<right){
int temp = nums[left];
nums[left++] = nums[right];
nums[right--] = temp;
}
}
return nums;
}
}
2 链表中倒数第k个节点
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
int i=1;
while(i<=k){
if(fast==null){
return null;
}
fast = fast.next;
i++;
}
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
3 反转链表
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode cur = null;
ListNode pre = null;
while(head!=null){
ListNode nxt = head.next;
head.next = pre;
pre = head;
head = nxt;
}
return pre;
}
}
4 合并两个排序的链表
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1==null&&list2!=null){
return null;
}
ListNode dummyNode = new ListNode(0);
ListNode head = new ListNode(0);
dummyNode = head;
while(list1!=null&&list2!=null){
if(list1.val<list2.val){
head.next = list1;
list1 = list1.next;
}else{
head.next = list2;
list2 = list2.next;
}
head = head.next;
}
while(list1!=null){
head.next = list1;
list1 = list1.next;
head = head.next;
}
while(list2!=null){
head.next = list2;
list2 = list2.next;
head = head.next;
}
head.next = null;
return dummyNode.next;
}
}
5 树的子结构
时间复杂度O(MN) 空间复杂度O(M) M为root1节点个数,N为root2节点个数
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null||root2==null){
return false;
}
return HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2)||recur(root1,root2);
}
public boolean recur(TreeNode root1,TreeNode root2){
//以下两个if判断不可以颠倒
if(root2==null){
return true;
}
if(root1==null){
return false;
}
if(root1.val==root2.val){
return recur(root1.left,root2.left)&&recur(root1.right,root2.right);
}else{
return false;
}
}
}
6 树的镜像
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot==null){
return null;
}
TreeNode left = Mirror(pRoot.left);
TreeNode right = Mirror(pRoot.right);
pRoot.left = right;
pRoot.right = left;
return pRoot;
}
}
7 顺时针打印矩阵
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> res = new ArrayList<>();
int top = 0;
int left = 0;
int right = matrix[0].length-1;
int bottom = matrix.length-1;
while(bottom>=top&&right>=left){
if(bottom==top){//横线或点
for(int i = left;i<=right;i++)
res.add( matrix[bottom][i]);
}else if(left==right){//竖线
for(int i = top;i<=bottom;i++)
res.add(matrix[i][left]);
}
else{
for(int i=left;i<right;i++){
res.add(matrix[top][i]);
}
for(int i=top;i<bottom;i++){
res.add(matrix[i][right]);
}
for(int i=right;i>left;i--){
res.add(matrix[bottom][i]);
}
for(int i=bottom;i>top;i--){
res.add(matrix[i][left]);
}
}
top++;
left++;
right--;
bottom--;
}
return res;
}
}
8 包含min函数的栈
class MinStack {
/** initialize your data structure here. */
private Stack<Integer> stack;
private Stack<Integer> help;
public MinStack() {
this.stack = new Stack<>();
this.help = new Stack<>();
}
public void push(int x) {
stack.push(x);
if(help.isEmpty()||help.peek()>=stack.peek()){
help.push(x);
}
}
public void pop() {
注意这里用equals!!
if(stack.pop().equals(help.peek()))
help.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return help.peek();
}
}