程序员面试金典编程学习

1.

import java.util.*;
 
public class Different {
    public boolean checkDifferent(String iniString) {
        // write code here
        char st = iniString.charAt(0);
        for(int i=1;i<iniString.length();i++){
            if(st == iniString.charAt(i)){
                return false;
            }
        }
        return true;
    }
}

2.

import java.util.*;
 
public class Reverse {
    public String reverseString(String iniString) {
        int len=iniString.length();
        if(len<=1||iniString==null)
            return iniString;
        StringBuffer sb=new StringBuffer();
        for(int i=len-1;i>=0;i--){
            sb.append(iniString.charAt(i));
        }
        return sb.toString();
        // write code here
    }
}

3.

import java.util.*;
 
public class Same {
    public boolean checkSam(String stringA, String stringB) {
        // write code here
        int lenA=stringA.length();
        int lenB=stringB.length();
        if(lenA!=lenB){
            return false;
        }else{
            int[] A=new int[256];
            int[] B=new int[256];
            for(int i=0;i<stringA.length();i++){
                A[stringA.charAt(i)]++;
            }
            for(int i=0;i<stringA.length();i++){
                B[stringB.charAt(i)]++;
            }
            for(int i=0;i<256;i++){
                if(A[i]!=B[i]){
                    return false;
                }
            }
             
        }
        return true;
    }
}

4.

import java.util.*;
 
public class Replacement {
    public String replaceSpace(String iniString, int length) {
        // write code here
        StringBuilder sb=new StringBuilder();
        String strReplace="%20";
        for(int i=0;i<length;i++){
            char tmp=iniString.charAt(i);
            if(tmp==' '){
                sb.append(strReplace);
            }else{
                sb.append(tmp);
            }
        }
        return sb.toString();
    }
}

5.

import java.util.*;
 
public class Zipper {
    public String zipString(String iniString) {
         
        int low = 0 , high = 0 ;
                 
        int len = iniString.length();
         
        StringBuilder sb = new StringBuilder();
          
        char c = ' ';
          
        int count = 0;
         
        while(low < len){
            high = low;
            c = iniString.charAt(low);
            while((high < len)&&(iniString.charAt(high) == c)){
                high ++;
            }
            count = high - low ;
            sb.append(c);
            sb.append(count);
            low = high;
        }
         
 
         
        return (sb.toString().length() < len)?sb.toString():iniString;
    }
}

6.

import java.util.*;
 
public class Transform {
    public int[][] transformImage(int[][] mat, int n) {
        // write code here
        int[][] a=new int[n][n];
       
        //第一步转置
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            a[j][i]=mat[i][j];
        }
    }
        //第二步换列
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            mat[i][j]=a[i][n-1-j];
        }
    }
    return mat;
    }
}

7.

import java.util.*;
 
public class Clearer {
    public int[][] clearZero(int[][] mat, int n) {
        // write code here
        if (mat == null)    return mat;
 
        boolean[] rows = new boolean[mat.length];
        boolean[] cols = new boolean[mat[0].length];
 
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                if (mat[i][j] == 0) {
                    rows[i] = true;
                    cols[j] = true;
                }
            }
        }
 
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                if (rows[i] || cols[j]) {
                    mat[i][j] = 0;
                }
            }
        }
 
        return mat;
    }
}

8.

import java.util.*;
 
public class ReverseEqual {
   public boolean checkReverseEqual(String s1, String s2) {
        // write code here
        byte[] s1arr = s1.getBytes();
        byte[] s2arr = s2.getBytes();
        Arrays.sort(s1arr);
        Arrays.sort(s2arr);
        if (Arrays.equals(s1arr,s2arr)){            
                return true;
        }
        return false;
    }
    
    
}

9.

/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode list,int k) {
if (list == null)   return list;
 
        ListNode node = list;
        int count = 0;
        while (node != null) {
            count++;
            node = node.next;
        }
        if (count < k)  return null;
 
        ListNode p = list;
        for (int i = 0; i < count - k; i++) {
            p = p.next;
        }
         
        return p;
    }
}

10.

import java.util.*;
 
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Remove {
    public boolean removeNode(ListNode pNode) {
        // write code here
        ListNode newNode = pNode;
        pNode = pNode.next;
        if(newNode.next==null) return false;
        else return true;
    }
}

11.

import java.util.*;
 
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null || pHead.next == null)
        {
            return pHead;
        }
        ListNode cur = pHead;
        ListNode Shead = new ListNode(-1);
        ListNode Bhead = new ListNode(-1);
        ListNode Stmp = Shead;
        ListNode Btmp = Bhead;
        while(cur != null){
            if(cur.val < x){
                Stmp.next = new ListNode(cur.val);
                Stmp = Stmp.next;
            }else{
                Btmp.next = new ListNode(cur.val);
                Btmp = Btmp.next;
            }
            cur = cur.next;
        }
        cur = Shead;
        while(cur.next != null && cur.next.val != -1){
            cur = cur.next;
        }
        cur.next = Bhead.next;
        return Shead.next;
         
         
         
    }
}

12.

import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Plus {
    public ListNode plusAB(ListNode a, ListNode b) {
        // write code here
         ListNode start = null;
         ListNode current = null;
         if(a == null && b == null) return start;
         int plus = 0;
         while(a!=null || b!= null){
             int tmp = plus;
             if(a!=null)
                 tmp += a.val;
             if(b!=null)
                 tmp+=b.val;
             if(start == null){
                 current = new ListNode(tmp%10);
                 start = current;
                 plus = tmp/10;
                  
             }else {
                current.next=new ListNode(tmp%10);
                current  = current.next;
                plus = tmp/10;
            }
             if(a==null&& b!=null)
                 b=b.next;
             else if(a!=null && b== null)
                 a=a.next;
             else {
                 a=a.next;
                 b=b.next;
            }
              
         }
        if(plus == 1)
             current.next = new ListNode(plus);
         
         return start;
    }
}

13.

import java.util.*;
 
/*
public class ListNode {
    int val;
    ListNode next = null;
 
    ListNode(int val) {
        this.val = val;
    }
}*/
public class Palindrome {
    public boolean isPalindrome(ListNode pHead) {
        ListNode pLength=pHead;
        ListNode p=pHead;
        int length=1;
        //求链表长度
        while(pLength.next!=null){
            length++;
            pLength=pLength.next;
        }
        Stack<Integer> sp=new Stack<Integer>();
        int nodeIndex=0;
        while(nodeIndex<=length/2-1)
        {
            sp.push(p.val);
            p=p.next;
            nodeIndex++;
        }
        int temp;
        //若长度为奇数
        if(length%2!=0)
            p=p.next;
        while(!sp.isEmpty())
        {
            temp=sp.pop();
            if(temp!=p.val)
                return false;
            p=p.next;
        }
        return true;
    }
}

14.

import java.util.*;
 
public class SetOfStacks {
    public ArrayList<ArrayList<Integer>> setOfStacks(int[][] ope, int size) {
        ArrayList<ArrayList<Integer>> SetOfStacks=new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> inner=new ArrayList<Integer>();
        SetOfStacks.add(inner);
        for(int i=0;i<ope.length;i++)
        {
            inner=SetOfStacks.get(SetOfStacks.size()-1);
            if(ope[i][0]==1)
            {
                if(inner.size()<size)
                    inner.add(ope[i][1]);
                else
                {
                    inner=new ArrayList<Integer>();
                    inner.add(ope[i][1]);
                    SetOfStacks.add(inner);
                }
            }
            if(ope[i][0]==2)
            {
                //若stack已经为空
                if(SetOfStacks.get(0).size()==0)
                    continue;
                if(inner.size()==0)
                {
                    SetOfStacks.remove(SetOfStacks.size()-1);
                    inner=SetOfStacks.get(SetOfStacks.size()-1);
                }
                inner.remove(inner.size()-1);
            }
        }
        return SetOfStacks;
    }
}

15.

import java.util.Stack;
 
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
     
    public void push(int node) {
        stack1.push(node);
    }
     
    public int pop() {
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        int first=stack2.pop();
        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }
        return first;
    }
}

16.

import java.util.*;
 
public class TwoStacks {
    public ArrayList<Integer> twoStacksSort(int[] numbers) {
        Stack<Integer> stack1=new Stack<Integer>();
        Stack<Integer> stack2=new Stack<Integer>();
         
        ArrayList<Integer>  list=new ArrayList<Integer>();
        for(int  i=0;i<numbers.length;i++)
            stack1.push(numbers[i]);
        while(!stack1.isEmpty())
        {
            //暂时取出stack1最顶端的 好让stack2中比他大的暂时进入stack1
            int temp=stack1.pop();
            while(!stack2.isEmpty() && stack2.peek()>temp)
            {
                stack1.push(stack2.pop());
            }
            stack2.push(temp);
        }
        while(!stack2.isEmpty())
        {
            list.add(stack2.pop());
        }
        return list;
    }
}

17.

import java.util.*;
 
public class CatDogAsylum {
    public ArrayList<Integer> asylum(int[][] ope) {
        Queue<Integer> dog=new LinkedList<Integer>();
        Queue<Integer> cat=new LinkedList<Integer>();
        Queue<Integer> all=new LinkedList<Integer>();
        ArrayList<Integer> animal=new ArrayList<Integer>();
        for(int i=0;i<ope.length;i++)
        {
            if(ope[i][0]==1)
            {
                all.add(ope[i][1]);
                if(ope[i][1]>0)
                    dog.add(ope[i][1]);
                else if(ope[i][1]<0)
                    cat.add(ope[i][1]);
            }
            else if(ope[i][0]==2)
            {
                if(ope[i][1]==0)
                {
                    if(!all.isEmpty())
                    {
                        //看最先被领养的是猫还是狗 同步删除猫或狗中的stack
                        int temp=all.poll();
                        animal.add(temp);
                        if(temp>0)
                            dog.poll();
                        else
                            cat.poll();
                    }
                }
                else if(ope[i][1]>0)
                {
                    if(!dog.isEmpty())
                    {
                        //看最先被领养的是猫还是狗 同步删除混合stack中的值
                        int temp=dog.poll();
                        animal.add(temp);
                        all.remove(temp);
                    }
                }
                else
                {
                    if(!cat.isEmpty())
                    {
                        //看最先被领养的是猫还是狗 同步删除混合stack中的值
                        int temp=cat.poll();
                        animal.add(temp);
                        all.remove(temp);
                    }
                }
            }
        }
        return animal;
    }
}

18.

import java.util.*;
 
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}*/
public class Balance {
  public boolean isBalance(TreeNode root) {
        // write code here
        if(root==null)
            return true;
        if(Math.abs(deepth(root.left)-deepth(root.right))>1)
            return false;
        else
            return isBalance(root.left)&isBalance(root.right);
    }
    public int deepth(TreeNode root){
        if(root==null)
            return 0;
        return Math.max(deepth(root.left),deepth(root.right))+1;
    }
}

19.

import java.util.*;
 
 
public class Path
{public boolean checkPath(UndirectedGraphNode a, UndirectedGraphNode b) {
    if (null == a || null == b)
        return false;
    HashSet<UndirectedGraphNode> map = new HashSet<UndirectedGraphNode>();
    boolean res = checkPathCore(a, b,map);
    map.clear();
    return  res||checkPathCore(b, a,map);
}
  
public boolean checkPathCore(UndirectedGraphNode a, UndirectedGraphNode b,HashSet<UndirectedGraphNode> map) {
    if (a == b)
        return true;
    map.add(a);// 标记a已被访问
    for (int i = 0; i < a.neighbors.size(); i++) {
        if(!map.contains(a.neighbors.get(i))&&checkPathCore(a.neighbors.get(i), b,map))
            return true;// 发现满足条件的路径立即返回,避免不必要的循环,导致超时。
    }
    return false;
}
     
}

20.

import java.util.*;
 
public class MinimalBST {
    public int buildMinimalBST(int[] vals) {
        int len = vals.length;
        if(len ==0){
            return 0;
        }else if(len ==1){
            return 1;
        }
        int a[] =new int[len/2];
        for(int i=0;i<len/2;i++){
            a[i]=vals[i];
        }
        return buildMinimalBST(a)+1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值