今天是美团测试面试,没想到给了我一道,层次遍历,我没写出来。因为我写代码就只是看书看答案,没有动手实践,这样导致了随便一个小问题,比如队列的具体实现,都能把我击垮,而且我没解决问题能力;这次侮辱折现出我的问题,眼高手低,没独立思考能力,而且手生了记性也差了。
任何事情一定要亲自动手独立的做一遍,光看书就废了。这是我的人生座右铭了。
不要光看实现,要多看代码实现的原理。
这里只写我自己写过得代码。
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
//[[1],[2,3],[4,5]]
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
//根节点为空,直接返回res
if(root == null)
return res;
if(root !=null){
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
ArrayList<Integer> level = new ArrayList<Integer>();
while(size-->0){
TreeNode temp = queue.poll();
level.add(temp.val);
if(temp.left!=null)
queue.offer(temp.left);
if(temp.right!=null)
queue.offer(temp.right);
}
res.add(level);
}
}
return res;
}
}
package com.sydx.aqsc.ctrl;
import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
}
class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
//[[1],[2,3],[4,5]]
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int a=q.size();
ArrayList<Integer> t=new ArrayList<>();
while(a--!=0){
TreeNode tmp=q.poll();
t.add(tmp.val);
if(tmp.left!=null)
q.add(tmp.left);
if(tmp.right!=null)
q.add(tmp.right);
}
res.add(t);
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class CBTInserter {
private Queue<TreeNode> queue;
private TreeNode root;
public CBTInserter(TreeNode root) {
this.root=root;
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
TreeNode tmp=q.poll();
if(tmp.left!=null)
q.add(tmp.left);
if(tmp.right!=null)
q.add(tmp.right);
}
}
public int insert(int v) {
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
TreeNode a =new TreeNode(v);
while(!q.isEmpty()){
TreeNode tmp=q.poll();
if(tmp.left!=null)
q.add(tmp.left);
else{
tmp.left=a;
return tmp.val;
}
if(tmp.right!=null)
q.add(tmp.right);
else{
tmp.right=a;
return tmp.val;
}
}
return 0;
}
public TreeNode get_root() {
return this.root;
}
}
/**
* Your CBTInserter object will be instantiated and called as such:
* CBTInserter obj = new CBTInserter(root);
* int param_1 = obj.insert(v);
* TreeNode param_2 = obj.get_root();
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class CBTInserter {
private Queue<TreeNode> queue;
private TreeNode root;
public CBTInserter(TreeNode root) {
this.root=root;
}
public int insert(int v) {
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
TreeNode a =new TreeNode(v);
while(!q.isEmpty()){
TreeNode tmp=q.poll();
if(tmp.left!=null)
q.add(tmp.left);
else{
tmp.left=a;
return tmp.val;
}
if(tmp.right!=null)
q.add(tmp.right);
else{
tmp.right=a;
return tmp.val;
}
}
return 0;
}
public TreeNode get_root() {
return this.root;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class CBTInserter {
private Queue<TreeNode> q;
private TreeNode root;
public CBTInserter(TreeNode root) {
this.root=root;
this.q=q;
Queue<TreeNode> q=new LinkedList<>();//重新赋值了
q.add(root);
while(q.peek().left!=null&&q.peek().right!=null){
TreeNode tmp=q.poll();
if(tmp.left!=null)
q.add(tmp.left);
if(tmp.right!=null)
q.add(tmp.right);
}
}
public int insert(int v) {
TreeNode parent= q.peek();
TreeNode node=new TreeNode(v);
if(parent.left==null) {
parent.left = node;
}
if(parent.right==null) {//错误
parent.right = node;
q.poll();
q.add(parent.left);
q.add(parent.right);
}
return parent.val;
}
public TreeNode get_root() {
return this.root;
}
}
/**
* Your CBTInserter object will be instantiated and called as such:
* CBTInserter obj = new CBTInserter(root);
* int param_1 = obj.insert(v);
* TreeNode param_2 = obj.get_root();
*/
{
private int[] cost;
public int minCostClimbingStairs(int[] cost) {
this.cost=cost;
int size=cost.length;
return Math.min(dfs(size-1), dfs(size-2));
}
private int dfs(int i) {
if(i<2)return cost[i];
return Math.min(dfs(i-1), dfs(i-2));
}
}
class Solution {
private int[] cost;
private int[] dp;
public int minCostClimbingStairs(int[] cost) {
this.cost=cost;
int size=cost.length;
dp=new int[size];
helper(size-1);
if(size==2)return Math.min(cost[1], cost[0]);
else if(size==1)return cost[0];
else return Math.min(dp[size-1], dp[size-2]);
}
private void helper(int i) {
if(i<2){
dp[i]=cost[i];
System.out.println(dp[i]);
}
else if(dp[i]==0){
helper( i-1);
helper(i-2);
dp[i]= Math.min(dp[i-1], dp[i-2])+cost[i];
}
}
}
class Solution {
public int divide(int a, int b) {
if (a == 0x80000000 && b == -1){
return Integer.MAX_VALUE;
}
if (a ==Integer.MAX_VALUE && b == 1){
return Integer.MAX_VALUE;
}
int nag=1;
if(a<0){
nag*=-1;
a=-a;
}
if(b<0){
nag*=-1;
b=-b;
}
int res=0;
while(a>=b){
int t=b;
int count=1;
while(a>=2*t){
count*=2;
t=t+t;
}
res+=count;
a=a-t;
}
return res*nag;
}
}
class Solution {
public String addBinary(String a, String b) {
StringBuilder result=new StringBuilder();
int i=a.length()-1;
int j=b.length()-1;
int jinwei=0;
while(i>=0||j>=0){
int digitalA=i>=0?a.charAt(i--)-'0':0;
int digitalB=j>=0?b.charAt(j--)-'0':0;
int sum=digitalA+digitalB +jinwei;
if(sum>=2) {
jinwei = 1;
result.append(sum-2);
}
else {
result.append(sum);
jinwei=0;
}
}
if(jinwei==1)
result.append(1);
return result.reverse().toString();
}
}
字符计算,分两种情况
class Solution {
public String addBinary(String a, String b) {
StringBuilder result=new StringBuilder();
int i=a.length()-1;
int j=b.length()-1;
int jinwei=0;
while(i>=0||j>=0){
char digitalA=i>=0?a.charAt(i--):'0';
char digitalB=j>=0?b.charAt(j--):'0';
int sum=digitalA-'0'+digitalB-'0'+jinwei;
System.out.println(sum);
if(sum>=2) {
result.append(sum-2);
jinwei = 1;
}
else {
result.append(sum);
jinwei=0;
}
}
if(jinwei==1)
result.append(1);
return result.reverse().toString();
}
}
循环+变量维护
class Solution {
public int[] countBits(int n) {
int count=0;
int[] res=new int[n+1];
while(n>=0){
int i=n;
while(i!=0){
count++;
i=i&(i-1);
}
res[n]=count;
count=0;
n--;
}
return res;
}
}
双循环提高维度 位移
class Solution {
public int singleNumber(int[] nums) {
int[] bit=new int[32];
for(int num:nums){
for(int i=0;i<32;i++){
int nume=num>>31-i;
bit[i]+=(nume&1);
}
}
int result=0;
for(int i=0;i<32;i++){
result=result*2+bit[i]%3;
}
return result;
}
}
class Solution {
public int[] twoSum(int[] numbers, int target) {
int i=0,j=numbers.length-1;
while(i<j){
int s=numbers[i]+numbers[j];
if(s<target){
i++;
}
else if(s>target){
j--;
}
else break;
}
return new int[]{i,j};
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res=new LinkedList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length-2;){
two(nums,i,res);
int temp=nums[i];
while(i<nums.length &&temp==nums[i]){
i++;
}
}
return res;
}
private void two(int[] nums, int i, List<List<Integer>> res) {
int m=i+1;
int n= nums.length-1;
while (m<n ){
if(nums[m]+nums[n]+nums[i]==0){
ArrayList<Integer> rt=new ArrayList<>();
rt.add(nums[i]);
rt.add(nums[m]);
rt.add(nums[n]);
res.add(rt);
int temp=nums[m];
while(m<n&&temp!=nums[m]){
m++;
}
}
else if(nums[m]+nums[n]+nums[i]<0){
m++;
}
else
n--;
}
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode slow=dummy;
ListNode fast=dummy;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next;
if(fast.next!=null)
fast=fast.next;
}
ListNode t=slow.next;
ListNode s=reverseList(t);
ListNode m=head;
while(s!=null){
if(m.val!=s.val)
return false;
m=m.next;
s=s.next;
}
return true;
}
private ListNode reverseList(ListNode head1) {
ListNode pre=null ;
ListNode cur=head1;
while(cur!=null){
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
}
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack=new Stack<>();
for(String token:tokens){
if(token.equals("*")||token.equals("/")||token.equals("+")||token.equals("-")){
int num2=stack.pop();
int num1=stack.pop();
if(token.equals("*")){
stack.push(num1*num2);
}
else if(token.equals("/")){
stack.push(num1/num2);
}
else if(token.equals("+")){
stack.push(num1+num2);
}
else if(token.equals("-")){
stack.push(num1-num2);
}
}
else
stack.push(Integer.parseInt(token));
}
return stack.pop();
}
}
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack<Integer> stack = new Stack<>();
for(int as:asteroids){
while (!stack.empty()&&stack.peek()<-as&&stack.peek()>0) {
stack.pop();
}
if(!stack.isEmpty()&&stack.peek()>0&&as==-1*stack.peek()){
stack.pop();
}
else if(stack.isEmpty()||as>0||stack.peek()<0){
stack.push(as);
}
}
int [] res=new int[stack.size()];
int i=0;
for (int s:stack){
res[i++]=s;
}
return res;
}
}