题目描述
给出两个有序的整数数组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;
}
}