nowcoder-oj【面试高频TOP榜单-简单难度(1)5道】
1、NC78 反转链表

/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { //自己首次实现(未实现) /* System.out.println(head.val); //1,head即为第一个数据节点 ListNode p = head; //游标p while(p.next != null){ p = p.next; } System.out.println(p.val); System.out.println(p.next); //null,p现已被移至尾节点 head.next = null; return p; */ //参考1 /* if(head == null || head.next == null){ return head; // 判断链表为空或长度为1的情况 } ListNode pre = null; // 当前节点的前一个节点 ListNode next = null; // 当前节点的下一个节点 while( head != null){ next = head.next; // 记录当前节点的下一个节点位置; head.next = pre; // 让当前节点指向前一个节点位置,完成反转 pre = head; // pre 往右走 head = next;// 当前节点往右继续走 } return pre; */ //参考2(p=上个的next) //初始化pre指针,用于记录当前结点的前一个结点地址 ListNode pre = null; //初始化p指针,用于记录当前结点的下一个结点地址 ListNode p = null; //head指向null时,循环终止。 while(head != null){ //先用p指针记录当前结点的下一个结点地址。 p = head.next; //让被当前结点与链表断开并指向前一个结点pre。 head.next = pre; //pre指针指向当前结点 pre = head; //head指向p(保存着原链表中head的下一个结点地址) head = p; } return pre;//当循环结束时,pre所指的就是反转链表的头结点 } }
参考1
参考2
2、NC61 两数之和

import java.util.*; public class Solution { /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here //0 /* int[] result = new int[2]; if(numbers.length == 2){ result[0] = 1; result[1] = 2; } for(int i=0; i<numbers.length; i++){ int num1 = numbers[i]; for(int j=i+1; j<numbers.length; j++){ int num2 = numbers[j]; if(num1+num2 == target){ result[0] = i+1; result[1] = j+1; } } } return result; */ //参考1:暴力破解 /* int length = numbers.length; for(int i=0; i<length-1; i++){ for(int j=i+1; j<length; j++){ if(numbers[i]+numbers[j] == target){ return new int[]{i+1, j+1}; } } } return new int[]{-1, -1}; */ //参考2:利用HashMap /* int length = numbers.length; Map<Integer, Integer> map = new HashMap<>(); for(int i=0; i<length; i++){ Integer temp = map.get(target-numbers[i]); if(temp != null){ return new int[]{temp+1, i+1}; //temp自动拆箱 } map.put(numbers[i], i); } return new int[]{0, 0}; */ //参考3:利用HashMap int length = numbers.length; int[] res = {0,0}; HashMap<Integer, Integer> map = new HashMap<>();//定义一个哈希表存储numbers[i]和对应的下标 for(int i=0 ; i<length; i++){ map.put(numbers[i],i); //进行标记 } for(int i=0; i<length; i++){ //每遍历一个numbers[i]就去对应的mp里找有没有target-numbers[i] //如果有就返回结果 //如果没有就找下一个 int temp = target-numbers[i]; if( map.containsKey(temp) && i!=map.get(temp) ){ res[0] = i+1; res[1] = map.get(temp)+1; return res; } } return res; } }
3、NC33 合并两个排序的链表

/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { //0(未实现) /* ListNode listRes; if(list1.val >= list2.val){ listRes = list1; }else{ listRes = list2; } // while(list1.next!=null & list2.next!=null){ // if(list1.val >= list2.val){ // listRes.next = list1; // }else{ // listRes.next = list2; // } // list1 = list1.next; // list2 = list2.next; // } while(true){ if(list1.val >= list2.val){ listRes.next = list1.next; }else{ listRes.next = list2.next; } if(list1.next == null){ listRes.next = list2; break; } if(list2.next == null){ listRes.next = list1; break; } list1 = list1.next; list2 = list2.next; } return listRes; */ //参考1 /* if(list1 == null){ return list2; } if(list2 == null){ return list1; } ListNode head = new ListNode(0); //这里不能指向Null,引用是要指向一个类型对象的,不然会报NullPointException ListNode moveNode = head; while(list1!=null && list2!=null){ if(list1.val > list2.val){ moveNode.next = list2; list2 = list2.next; }else{ moveNode.next = list1; list1 = list1.next; } moveNode = moveNode.next; } if(list1 != null){ moveNode.next = list1; } if(list2 != null){ moveNode.next = list2; } return head.next; */ //参考2 if(list1 == null){ return list2; } if(list2 == null){ return list1; } ListNode dum = new ListNode(0); // 定义哑节点 ListNode result = dum; // 记录一下头节点 while(list1!=null && list2!=null) { // 当连个链表都没有遍历完时 if (list1.val > list2.val) { // 值小的一方先放入结果链表中 dum.next = list2; list2 = list2.next; } else { dum.next = list1; list1 = list1.next; } dum = dum.next; } dum.next = (list1 == null)?list2:list1; // 将剩余没有遍历放入结果链表中 return result.next; // 返回头节点 } }
4、NC76 用两个栈实现队列

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); } //push时间复杂度O(1) public int pop() { if (stack2.size() <= 0) { while (stack1.size() != 0) { stack2.push(stack1.pop()); } } return stack2.pop(); } //pop时间复杂度O(1) }
5、NC68 跳台阶

public class Solution { //参考1:简单递归(这种当n比较大的时候会超时,所以不推荐) /* public int jumpFloor(int target) { if(target <= 3){ return target; }else{ return jumpFloor(target-1) + jumpFloor(target-2); } } */ //参考2:递归改进-尾递归 /* public int jumpFloor(int target) { return Fibonacci(target, 1, 1); } public int Fibonacci(int n, int a, int b){ if(n <= 1){ return b; }else{ return Fibonacci(n-1, b, a+b); } } */ //参考3:非递归(递归由于重复计算,导致效率很差) /* public int jumpFloor(int target) { if(target <= 1){ return 1; } int[] arr = new int[target+1]; arr[1] = 1; arr[2] = 2; for(int i=3; i<=target; i++){ arr[i] = arr[i-1] + arr[i-2]; } return arr[target]; } */ //参考4:非递归改进(看到上面的数组当前值是依赖他前面两个值的(前两个除外),我们只需要用两个临时变量即可,不需要申请一个数组) public int jumpFloor(int target) { if(target <= 2){ return target; } int first = 1; int second = 2; int sum = 0; while(target-- > 2){ sum = first + second; first = second; second = sum; } return sum; } }
标签:
Online Judge
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!