剑指offer简单题01-10

01
在这里插入图片描述

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param numbers int整型一维数组 
     * @return int整型
     */
    public int duplicate (int[] numbers) {
        // write code here
        int [] arr = new int[numbers.length];
        for(int i=0;i<numbers.length;i++)
        {
            arr[numbers[i]]++;
            if(arr[numbers[i]]==2){
                return numbers[i];
            }
        }
        return -1;
    }
}

02
在这里插入图片描述

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @return string字符串
     */
    public String replaceSpace (String s) {
        StringBuilder str = new StringBuilder();
        // write code here
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)==' ')
                str.append("%20");
            else
                str.append(s.charAt(i));
        }
        return str.toString();
    }
}

知识点:
在这里插入图片描述

03
在这里插入图片描述

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
       ArrayList<Integer> list = new ArrayList<>();
       ListNode tmp = listNode;
       while(tmp!=null)
       {
        list.add(0,tmp.val);//每次都插入列表的头部,最终得到逆序列表
        tmp = tmp.next;
       }
       return list;
    }
}

或者递归:

import java.util.*;
public class Solution {
    ArrayList<Integer> list = new ArrayList();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode!=null){
            printListFromTailToHead(listNode.next);
            list.add(listNode.val);
        }
        return list;
    }
}

04
在这里插入图片描述

import java.util.*;
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.empty()){
            stack2.push(stack1.pop());
        }

        int res = stack2.pop();

        while(!stack2.empty()){
            stack1.push(stack2.pop());
        }
        return res;
    } 
}

题解中的思路:注意最后从栈2中出栈的元素还得入栈1.
在这里插入图片描述
05
在这里插入图片描述

遍历:时间复杂度o(n)

import java.util.*;

public class Solution {
    public int minNumberInRotateArray (int[] nums) {
        // write code here
        for(int i=0;i<nums.length-1;i++){
            if(nums[i]>nums[i+1])
                return nums[i+1];
        }
        return nums[0];
    }
}

二分法:时间复杂度o(logn)

import java.util.*;

public class Solution {
    public int minNumberInRotateArray (int[] nums) {
        if(nums.length==0)
            return 0;
        int i=0, j=nums.length-1;
        while(i<j)
        {
            int mid = (i+j)/2;
            if(nums[mid]>nums[j]) i=mid+1;
            else if(nums[mid]<nums[j]) j=mid;
            else j--;//应对连续重复的情况
        }
        return nums[i];
    }
}

06
在这里插入图片描述

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return int整型
     */
    public int NumberOf1 (int n) {
        int res = 0;
        // write code here
        for(int i=0;i<32;i++)
        {
            if((n&(1<<i))!=0)
                res++;
        }
        return res;
    }
}

在这里插入图片描述

07
在这里插入图片描述

import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 最大位数
     * @return int整型一维数组
     */
    public int[] printNumbers (int n) {
        int c = (int)Math.pow(10,n);
        int [] arr = new int[c-1];
        // write code here
        for(int i=1;i<c;i++)
        {
            arr[i-1] = i;
        }
        return arr;
    }
}

08
在这里插入图片描述

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param val int整型 
     * @return ListNode类
     */
    public ListNode deleteNode (ListNode head, int val) {
        // write code here
        //加入一个头结点
        ListNode res = new ListNode(0);
        res.next = head;
        ListNode pre = res;
        ListNode cur = head;
       
        while(cur!=null){
            if(cur.val==val){
                pre.next=cur.next;
                cur = cur.next;
            }
            else{
                pre=cur;
                cur=cur.next;
            }
        }
        return res.next;
    }
}

思路:重点是设置一个头结点,便于删除和返回。

09
在这里插入图片描述
解法:快慢指针,快指针先走K步,慢指针再开始走。那么当快指针走完的时候,慢指针刚好指向倒数第K个节点。

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode FindKthToTail (ListNode pHead, int k) {
        // write code here
        ListNode fast = pHead;
        ListNode slow = pHead;
        for(int i=0;i<k;i++)
        {
            if(fast!=null) fast=fast.next;
            else return null;//说明总长度不足K步,返回Null
        }
        //快慢指针一起走,快指针到尾,慢指针到倒数K
        while(fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}

10
在这里插入图片描述
在这里插入图片描述

import java.util.Stack;
public class Solution {
    public ListNode ReverseList(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        //把链表节点全部摘掉放到栈中
        while(head!=null)
        {
            stack.push(head);
            head = head.next;
        }
        if(stack.isEmpty()) return null;

        //栈中的结点全部出栈,然后重新连成一个新的链表
        ListNode res = stack.pop();
        ListNode cur = res;
        while(!stack.isEmpty()){
            cur.next = stack.pop();
            cur = cur.next;
        }
        //最后一个结点就是反转前的头结点,一定要让他的next等于空,否则会构成环
        cur.next = null;
        return res;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值