20190730算法题存档
题目描述
Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…
You must do this in-place without altering the nodes' values.
For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.
public class Solution {public void reorderList(ListNode head) {if(head == null || head.next == null) {return;}ListNode slow = head;ListNode quick = head;while(quick.next != null && quick.next.next != null) {slow = slow.next;quick = quick.next.next;}ListNode mid = slow.next;if(mid != null && mid.next != null) {ListNode next = mid.next;while(next != null) {mid.next = next.next;next.next = slow.next;slow.next = next;next = mid.next;}}ListNode p = head;ListNode q = slow.next;while(q != null && p != null){slow.next = q.next;q.next = p.next;p.next = q;p = p.next.next;q = slow.next;}}
}
题目描述
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
public class Solution {public ListNode detectCycle(ListNode head) {if (head != null) {ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {fast = head;while (slow != fast) {slow = slow.next;fast = fast.next;}return slow;}}}return null;}
}
题目描述
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
public class Solution {public boolean hasCycle(ListNode head) {if(head != null) {ListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if(fast == slow) {return true;}}}return false;}
}
题目描述
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s ="leetcode",
dict =["leet", "code"].
Return true because"leetcode"can be segmented as"leet code".
public class Solution {public boolean wordBreak(String s, Set<String> dict) {boolean[] temp = new boolean[s.length() + 1];temp[0] = true;for(int i = 1; i < temp.length; i++) {for(int j = 0; j < i; j++) {if (temp[j] && dict.contains(s.substring(j, i))){temp[i] = true;break;}}}return temp[temp.length - 1];}
}
题目描述
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
public class Solution {public RandomListNode copyRandomList(RandomListNode head) {if(head == null || (head.next == null && head.random == null)){return head;}RandomListNode node = head;while(node != null){RandomListNode copy = new RandomListNode(node.label);copy.next = node.next;copy.random = node.random;node.next = copy;node = node.next.next;}node = head.next;while(node != null){if(node.next != null){node.next = node.next.next;}if(node.random != null){node.random = node.random.next;}node = node.next;}return head.next;}}
20190730算法题存档相关推荐
- 算法题存档20200505
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- 算法题存档20191223
题目描述 删除给出链表中的重复元素,使链表中的所有元素都只出现一次 例如: 给出的链表为1->1->2,返回1->2. 给出的链表为1->1->2->3->3 ...
- 算法题存档20190207
题目描述 如果一个整数只能被1和自己整除,就称这个数是素数. 如果一个数正着反着都是一样,就称为这个数是回文数.例如:6, 66, 606, 6666 如果一个数字既是素数也是回文数,就称这个数是回文 ...
- 算法题存档20190127
题目描述 假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径.迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路.迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与 ...
- 算法题存档20200627(树)
给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表. 如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 Tru ...
- 算法题存档2020425
给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3, ...
- 20191219算法题存档
题目描述 给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组 注意: 可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n public class ...
- 20190724算法题存档
题目描述 Sort a linked list in O(n log n) time using constant space complexity. public class Solution {p ...
- 20190719算法题存档
题目描述 小Q得到一个神奇的数列: 1, 12, 123,...12345678910,1234567891011.... 并且小Q对于能否被3整除这个性质很感兴趣. 小Q现在希望你能帮他计算一下从数 ...
最新文章
- 第三次作业+105032014085
- 为什么大公司一定要使用DevOps?
- select chosen-jquery.js下拉列表的层被覆盖
- 2018年中国视频监控行业现状及行业发展趋势分析预测【图】
- so baby come on~~
- 在CentOS 7上安装使用Kubernetes:管理云平台多个主机上的容器化应用
- vue-count-to插件使用方法
- const成员函数、inline成员函数、static成员函数中关键字的位置
- 如何对CAD图纸快速测量?
- MyBatis官网下载步骤
- JavaEclipse中从Lambada表达式运行错误的解决方法到学到新知识
- MySQL中的删除:drop,delete,truncate的区别和联系
- 计算机专业如何自己增加项目经验?
- Dcloud学习资料汇总+视频教程
- qt网络编程及readyread信号
- UIPickerView 用法
- 查询Apple app的bundle ID
- js控制浏览器全屏显示
- DFS(深度优先搜索)学习笔记(C语言版本)
- 关于解决2345流氓软件,默认打开2345网址的问题