Problem 1:
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
1. Search target from the bottom-left elements of array (same as from top-right elements)
public class Solution {
public boolean Find(int target, int [][] array) {
int rows = array.length;
int cols = array[0].length;
if(rows == 0 && cols == 0)
return false;
int row = rows - 1;
int col = 0;
while(row >= 0 && col < cols)
{
if(array[row][col] == target)
return true;
else if(array[row][col] > target)
row--;
else
col++;
}
return false;
}
}
Time complexity : O(width + height)
Space complexity: O(1)
2. Recursion(cross division, out of Memory)
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
if(array.size() == 0 || array[0].size() == 0)
return false;
else
cross_division(array, target, 0, 0, array.size() - 1, array.size() - 1);
}
bool cross_division(vector<vector<int> > &arr, int target, int top, int left, int bottom, int right)
{
int cx = (top + bottom) / 2;
int cy = (left + right) / 2;
if(top == bottom && left == right)
if(arr[top][left] == target)
return true;
else
return false;
if(arr[top][left] > target || arr[bottom][right] < target)
return false;
if(arr[cx][cy] == target)
return true;
else if(arr[cx][cy] < target)
cross_division(arr, target, cx, cy, bottom, right);
else
cross_division(arr, target, top, left, cx, cy);
cross_division(arr, target, cx, left, cy, bottom);
cross_division(arr, target, top, cy, cx, right);
return false;
}
};
3.Binary Search(Runtime Error: stack overflow)???
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
for(int i = 0; i < array.size(); i++)
{
if(array.empty())
return false;
int low = 0;
int high = array[i].size() - 1;
if(array[i][low] <= target && target <= array[i][high])
{
while(low <= high)
{
int mid = (low + high) / 2;
if(mid == target)
return true;
else if(mid < target)
low = mid + 1;
else
high = mid - 1;
}
}
}
return false;
}
};
Useful knowlege:
Initialize a vector in C++ (5 different ways)
Initialize a 2D vector in C++ with user defined size
Problem 2:
输入一个链表,按链表从尾到头的顺序返回一个ArrayList
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
ListNode temp = listNode;
while(temp != null)
{
list.add(0,temp.val);
temp = temp.next;
}
return list;
}
}
2. Recursion
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list = new ArrayList<>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode != null)
{
printListFromTailToHead(listNode.next);
list.add(listNode.val);
}
return list;
}
}
Useful Knowledge:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Problem 3:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Arrays;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if (pre.length == 0 || in.length == 0)
return null;
TreeNode root = new TreeNode(pre[0]);
for (int i = 0; i < in.length; i++)
{
if (in[i] == pre[0])
{
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
break;
}
}
return root;
}
}
Time complexity : O(n)
Space complexity: O(n)
Problem 4:
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
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);
}
public int pop() {
if(stack2.empty()){
while(!stack1.empty())
stack2.push(stack1.pop());
}
return stack2.pop();
}
}