20191219算法题存档
题目描述
给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组
注意:
可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n
public class Solution {public void merge(int A[], int m, int B[], int n) {int tempA = --m;int tempB = --n;while(tempA >= 0 && tempB >= 0) {if(A[tempA] >= B[tempB]) {A[tempA + tempB + 1] = A[tempA--];} else {A[tempA + tempB + 1] = B[tempB--];}}if(tempB >= 0) {for(int i = 0; i < tempB + 1; i++) {A[i] = B[i];}}}
}
题目描述
题目给出一个字符串s1,我们可以用递归的方法将字符串分成两个非空的子串来将s1表示成一个二叉树
下面是s1=“great”的一种二叉树的表现形式:
great↵ / ↵ gr eat↵ / / ↵g r e at↵ / ↵ a t
将字符串乱序的方法是:选择任意的非叶子节点,交换它的两个孩子节点。
例如:如果我们选择节点“gr”交换他的两个孩子节点,就会产生一个乱序字符串"rgeat".
rgeat↵ / ↵ rg eat↵ / / ↵r g e at↵ / ↵ a t
我们称"rgeat"是"great"的一个乱序字符串。
类似的:如果我们继续交换“eat”的两个孩子节点和“at”的两个孩子节点,会产生乱序字符串"rgtae".
rgtae↵ / ↵ rg tae↵ / / ↵r g ta e↵ / ↵ t a
我们称"rgtae"是"great"的一个乱序字符串。
给出两个长度相同的字符串s1 和 s2,请判断s2是否是s1的乱序字符串。
public class Solution {public boolean isScramble(String s1, String s2) {if(s1.equals(s2)) {return true;}int[] letters = new int[26];for (int i = 0; i < s1.length(); i++) {letters[s1.charAt(i) - 'a']++;letters[s2.charAt(i) - 'a']--;}for (int i = 0; i < 26; i++) {if (letters[i] != 0) {return false;}}for (int i = 1; i < s1.length(); i++) {if (isScramble(s1.substring(0, i), s2.substring(0, i))&& isScramble(s1.substring(i), s2.substring(i))) {return true;}if (isScramble(s1.substring(0, i), s2.substring(s2.length() - i))&& isScramble(s1.substring(i), s2.substring(0, s2.length() - i))) {return true;}}return false;}
}
题目描述
给出一个链表和一个值x,以x为参照将链表划分成两部分,使所有小于x的节点都位于大于或等于x的节点之前。
两个部分之内的节点之间要保持的原始相对顺序。
例如:
给出1->4->3->2->5->2和x = 3,
返回1->2->2->4->3->5.
public class Solution {public ListNode partition(ListNode head, int x) {ListNode rightHead = new ListNode(0);ListNode leftHead = new ListNode(0);ListNode right = rightHead;ListNode left = leftHead;while (head != null) {if (head.val < x) {right.next = head;right = right.next;} else {left.next = head;left = left.next;}head = head.next;}right.next = leftHead.next;left.next = null;return rightHead.next;}
}
题目描述
给出一个只包含0和1的二维矩阵,找出最大的全部元素都是1的长方形区域,返回该区域的面积。
public class Solution {public int maximalRectangle(char[][] matrix) {int result = 0;if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {return result;}int y = matrix.length;int x = matrix[0].length;int[] singleHigh = new int[x];for(int i = 0; i < y; i++) {for(int j = 0; j < x; j++) {if(matrix[i][j] == '1') {singleHigh[j] += 1;} else {singleHigh[j] = 0;}}Stack<Integer> stack = new Stack<>();stack.push(-1);for (int j = 0; j < x; j++) {while (stack.peek() != -1 && singleHigh[j] < singleHigh[stack.peek()]) {int top = stack.pop();result = Math.max(result, (j - 1 - stack.peek()) * singleHigh[top]);}stack.push(j);}while (stack.peek() != -1) {int top = stack.pop();result = Math.max(result, (x - 1 - stack.peek()) * singleHigh[top]);}}return result;}
}
题目描述
给出n个数字,代表直方图的条高,直方图每一条的宽度为1,请计算直方图中最大矩形的面积
public class Solution {public int largestRectangleArea(int[] height) {int result = 0;Stack<Integer> stack = new Stack<>();stack.push(-1);for (int j = 0; j < height.length; j++) {while (stack.peek() != -1 && height[j] < height[stack.peek()]) {int top = stack.pop();result = Math.max(result, (j - 1 - stack.peek()) * height[top]);}stack.push(j);}while (stack.peek() != -1) {int top = stack.pop();result = Math.max(result, (height.length - 1 - stack.peek()) * height[top]);}return result;}
}
题目描述
给出一个排好序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为1->2->3->3->4->4->5, 返回1->2->5.
给出的链表为1->1->1->2->3, 返回2->3.
public class Solution {public ListNode deleteDuplicates(ListNode head) {if(head == null || head.next == null) {return head;}ListNode first = new ListNode(-1);first.next = head;ListNode pre = first;ListNode now = head;while(now != null && now.next != null){if(now.val != now.next.val){pre = now;}else{while(now.next != null && now.val == now.next.val) {now = now.next;}pre.next = now.next;}now = now.next;}return first.next;}}
20191219算法题存档相关推荐
- 算法题存档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, ...
- 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→ ...
- 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现在希望你能帮他计算一下从数 ...
最新文章
- 从校园情侣到教授夫妇,520当天他们携手发顶刊!
- 表格元素的快捷获取以及隔行变色、鼠标移入变色案例
- Oracle关于java.sql.SQLException常见错误集锦
- biweb wms门户网站php开源建站系统 v5.8.3,BIWEB WMS PHP开源企业建站系统 v5.8.5
- html之属性的定义
- iptables详解001:iptables概念
- 武汉理工大学刷课,刷在线作业程序,做作业脚本
- 实习商汤,校招华为,我的深度学习之路
- 基于CPU+GPU的H.264编码器并行编码设计
- [已解决]Could not create connection to database server.错误的解决方法
- ipad查看本地文件html文件在哪里,ipad文件夹在哪里?小编手把手教你找到ipad文件夹在哪里...
- 天天向上的力量python代码解释_天天向上的力量 B
- idea 引入包报错:Unable to provision, see the following errors
- C++培训_001_WIN10的安装与激活_VS编译器的安装
- pcl计算点云法向量
- 从[USER SERVICES CLIENT]下载的sentinel-3数据在SNAP中打开没有地理坐标的解决办法
- 搜狗浏览器和360浏览器css布局注意事项
- android多语言切换失效
- 计算机英语教程第五版答案解析,计算机专业英语教程(第5版)翻译完整版
- python重定向是什么意思_重定向 - Python.M - 博客园
热门文章
- transferto遇到的问题java.io.FileNotFoundException: C:\Users\Administrator\AppData\Local\Temp
- 新代系统cnc怎样连接电脑_你真的了解3C产线上的运控系统吗?
- 华视读卡器多浏览器插件_翻遍Chrome商店,这9款插件值得安装
- ContextLoaderListener的作用详解
- springmvc进不到controller_Spring、SpringMVC、MyBatis的整合
- [linux]关于deepin截图软件在KDE桌面下无法使用粘贴的解决方法
- leetcode-507-Perfect Number
- linux中高端内存和低端内存的概念【转】
- php设计模式之单例模式 1
- auto_cmdb--01之models.py建表