2020-8-26 剑指offer编程小哥令狐 075211

简介: 2020-8-26 剑指offer编程小哥令狐 075211

剑指offer~编程小哥令狐

一、数组类~

03、数组中重复的数字

class Solution
 {
    public void swap(int[] nums,int i,int j)
    {
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
    public int findRepeatNumber(int[] nums) 
    {
        int n=nums.length;
        for(int num:nums)
            if(num<0||num>=n)
                return -1;
        
        for(int i=0;i<n;i++)
        {
            while(nums[i]!=i&&nums[i]!=nums[nums[i]])
                swap(nums,i,nums[i]);
            if(nums[i]!=i&&nums[i]==nums[nums[i]])
                   return nums[i];
        }
         return -1;
    }
}

04、二维数组中的查找

  • 暴力查找
class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int i,j;
        if((matrix==null||matrix.length==0)||(matrix.length==1&&matrix[0].length==0))
            return false;
        for (i=0;i<matrix.length;i++)
            for (j=0;j<matrix[0].length;j++)
                if(matrix[i][j]==target)
                    return true;
        
        return false;
    } 
}
  • 线性查找
class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if((matrix==null||matrix.length==0)||(matrix.length==1&&matrix[0].length==0))
            return false;
        int i=0,j=matrix[0].length-1;
        while(i<=matrix.length-1&&j>=0){
            if(target==matrix[i][j]) return true;
            if(target>matrix[i][j]) i++;
            else if(target<matrix[i][j]) j--;
        }
        return false;
    } 
}

05、顺时针打印矩阵

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        //判断边界
        if(matrix.length==0)
            return new int[0];//因为返回值是数组,所以实例化一个数组进行返回
        //右下左上
        int left=0,right=matrix[0].length-1,up=0,down=matrix.length-1;
        int num=0;
        int []res=new int[(right+1)*(down+1)];
        while(true){
            //右
            for(int i=left;i<=right;i++)
                res[num++]=matrix[up][i];
            if(++up>down) break;///判断边界
            //下
            for(int i=up;i<=down;i++)
                res[num++]=matrix[i][right];
            if(--right<left) break;
            //左
            for(int i=right;i>=left;i--)
                res[num++]=matrix[down][i];
            if(--down<up) break;
            //上
            for(int i=down;i>=up;i--)
                res[num++]=matrix[i][left];
            if(++left>right) break;
        }
        return res;
    }
}

06、在排序数组中查找数字

  • 暴力查找法
class Solution {
    public int search(int[] nums, int target) {
        int res=0;
        for(int i=0;i<nums.length;i++)
                if(nums[i]==target)
                    res++;
        return res;
            
    }
}
  • 二分查找法【有序数组优先考虑二分查找】
class Solution {
    public int search(int[] nums, int target) {
       int left = 0, right = nums.length;
        int ans = 0;
        while (left < right){
            int mid = left + ((right - left)/2);
            if (nums[mid] >= target){
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        while (left < nums.length && nums[left++] == target){
            ans++;
        }
        return ans;
            
    }
}

07、剑指 Offer 53 - II. 0~n-1中缺失的数字

class Solution {
    public int missingNumber(int[] nums) {
         int left = 0, right = nums.length;
        while(left < right){
            int mid = left + (right - left) / 2;
            if (nums[mid] == mid) {
                left = mid + 1;
            } else {
                right = mid;
            }  
        }
        return left;
    }
}

二、链表类~

06、从尾到头打印链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode temp=head;
        int size=0;
        while(temp!=null){
            size++;
            temp=temp.next;
        }
        int[] res=new int[size];
        temp=head;
        for(int i=size-1;i>=0;i--){
            res[i]=temp.val;
            temp=temp.next;
        }
        return res;
    }
}

07、删除链表节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if(head.val==val) return head.next;
        ListNode pre=head;
        ListNode cur=head.next;
        while(cur.val!=val&&cur!=null){
            pre=cur;
            cur=cur.next;
        }
        if(cur.val==val){
            pre.next=cur.next;
        }
        return head;
    }
}

08、删除倒数第k个节点

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zjjaWHAc-1599804497564)(https://cdn.jsdelivr.net/gh/JackieLing/mage1/img/20200905211117.png)]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode current=head;
        int n=0;
        while(current!=null){
            n++;
            current=current.next;
        }
        if(k>n) return null;
        int aim=n-k+1;
        int ptr=1;
        current=head;
        while(ptr<aim){
            current=current.next;
            ptr++;
        }
        return current;
    }
}

09、反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
         ListNode pre=null;
        ListNode cur=head;
        while (cur!=null){
            ListNode temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;
        }
        return pre; 
    }
}

10、复杂链表的复制

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;
    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        Node cur=head;
        HashMap<Node,Node> map=new HashMap<>();
        while(cur!=null){
            map.put(cur,new Node(cur.val));
            cur=cur.next;
        }
        cur=head;
        while(cur!=null){
            map.get(cur).next=map.get(cur.next);
            map.get(cur).random=map.get(cur.random);
            cur=cur.next;
        }
        return map.get(head);
    }
}

11、两个链表的第一个公共点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a=headA;
        ListNode b=headB;
        while(a!=b){
            if(a==null)
                a=headB;
            else
                a=a.next;
            
            if(b==null)
                b=headA;
            else
                b=b.next;
        }
        return a;
    }
}

三、哈希表~

12、数组中重复的数字

class Solution
 {
    public int findRepeatNumber(int[] nums) 
    {
        Set<Integer> set=new HashSet<Integer>();
        int repeat=0;
        for(int i=0;i<nums.length;i++){
            if(!set.add(nums[i])){
                repeat=nums[i];
                break;
            }
        }
        return repeat;
    }
}

13、最长不含重复字符的子字符串

class Solution {
    public int lengthOfLongestSubstring(String s) {
         HashMap<Character,Integer> hash=new HashMap<>();
        int res=0,left=0;
        for (int i=0;i<s.length();i++){
            char c=s.charAt(i);
            //判断c是否出现过
            //abcabcbb
            if (hash.containsKey(c)){
                left=Math.max(left,hash.get(c)+1);
            }
            hash.put(c,i);
            res=Math.max(res,i-left+1);
        }
        return res;
    }
}

14、第一个只出现一次的字符

class Solution {
    public char firstUniqChar(String s) {
       HashMap<Character,Boolean> hash=new HashMap<>();
       char []ch=s.toCharArray();
       for(char c:ch)
            hash.put(c,!hash.containsKey(c));
        for(char c:ch)
            if(hash.get(c))
                return c;
        return ' ';
        
    }
}

四、栈~

15、用两个栈实现队列

import java.util.HashMap;
import java.util.Stack;
class CQueue {
    //栈 先进后出
    //队列 先进先出
    //Stack<Integer> stk1,stk2;
       Stack<Integer> stk1=new Stack<Integer>();
         Stack<Integer> stk2=new Stack<Integer>();
    int size;
    public CQueue() {
        //stk1=new Stack<Integer>();
        //stk2=new Stack<Integer>();
        size=0;
    }
    public void appendTail(int value) {
        //插入一个元素
        //stk1保存 底部存新插入的  顶部存老的
        while(!stk1.isEmpty())
            stk2.push(stk1.pop());
        stk1.push(value);
        while (!stk2.isEmpty())
            stk1.push(stk2.pop());
        size++;
    }
    public int deleteHead() {
        //删除队列首部元素
        //删除栈顶
        if(size==0)
            return -1;
        int res=stk1.pop();
        size--;
        return res;
    }
}
/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */


目录
相关文章
|
前端开发
#yyds干货盘点# 前端歌谣的刷题之路-第一百一十七题-实现斐波那契数列
#yyds干货盘点# 前端歌谣的刷题之路-第一百一十七题-实现斐波那契数列
100 0
#yyds干货盘点# 前端歌谣的刷题之路-第一百一十七题-实现斐波那契数列
|
3月前
|
算法 安全 NoSQL
2024重生之回溯数据结构与算法系列学习之顺序表习题精讲【无论是王道考研人还真爱粉都能包会的;不然别给我家鸽鸽丢脸好嘛?】
顺序表的定义和基本操作之插入;删除;按值查找;按位查找习题精讲等具体详解步骤以及举例说明
|
Java
技术面试中常见的几道智力题 来看看你会做几道?
下面是大部分题目来自滴滴出行2017秋招题。有几道题目是我在做的时候没有想出来的,还有几道题目整理在这里单纯是为说明有一些智力方向的面试题并不是大家想的那么难,我们运用高中的知识就完全可以解决。
4972 0
|
算法 C语言 C++
【C语言蓝桥杯每日一题】——跑步锻炼
  哈喽各位友友们😊,我今天又学到了很多有趣的知识,现在迫不及待的想和大家分享一下!😘我仅已此文,和大家分享【C语言蓝桥杯每日一题】——跑步锻炼~ 都是精华内容,可不要错过哟!!!😍😍😍
138 0
|
前端开发 JavaScript
#yyds干货盘点# 前端歌谣的刷题之路-第四十五题-数组反转
#yyds干货盘点# 前端歌谣的刷题之路-第四十五题-数组反转
101 0
#yyds干货盘点# 前端歌谣的刷题之路-第四十五题-数组反转
|
9月前
|
算法 搜索推荐 程序员
C语言十九练——养兔子
C语言十九练——养兔子
89 0
|
程序员
一位40多岁骨灰级程序员的干货分享!
前段时间在极客时间上购买了 Service Mesh 课程,并在我们Java技术栈知识星球VIP微信群中进行了分享,提供给球友免费阅读。 今天,我又花了199元订阅了极客时间上一位大咖的技术专栏(左耳朵耗子),这真是个牛人,四十多岁的骨灰级程序员!看了下他的个人介绍很震撼,分享的技术知识体系也非常不错,现在特别推荐给大家。
1297 0
|
Windows
【牛客刷题】/*日常四道编程小题分享*/
【牛客刷题】/*日常四道编程小题分享*/
130 0
【牛客刷题】/*日常四道编程小题分享*/
|
人工智能
[Offer收割]编程练习赛3 - 题目3 : 智力竞赛
智力竞赛  Problem's Link  ---------------------------------------------------------------------------- Mean:  略(中文题).
919 0
|
机器学习/深度学习 人工智能 程序员
2023年 团体程序设计天梯赛个人感悟及总结(附题解)——遗憾国三
⭐L1一阶题 ⭐L1-089 最好的文档 (5分)—水题 👉👉👉👉👉👉L1-089 最好的文档👈👈👈👈👈👈 有一位软件工程师说过一句很有道理的话:“Good code is its own best documentation.”(好代码本身就是最好的文档)。本题就请你直接在屏幕上输出这句话。 输入格式: 本题没有输入。 输出格式: 在一行中输出 Good code is its own best documentation.。 输入样例: 无 输出样例: Good code is its own best documentation.
833 0

热门文章

最新文章