本博客试题均使用javascript实现
思路
:从二维数组右上开始比较,若该数大于传入整数,则向左查找;若该数小于传入整数,则向下查找。代码
:function Find(target, array){
var row = 0;
var col = array[0].length-1;
while(row <= array.length-1 && col >= 0){
if(array[row][col] > target){
col--;
}else if(array[row][col] < target){
row++;
}else{
return true;
}
}
return false;
}
思路
:使用replace方法可以直接替换,这里使用split方法将原字符串每一项切割为数组并创建新数组,遍历原数组,若当前项为空格,则在新数组中添加新元素"%20";若当前项不为空格,则在新数组添加当前项,最后通过join方法将数组拼接成字符串。代码
:function replaceSpace(str){
var oldArr = str.split("");
var newArr = [];
for(index in oldArr){
if(oldArr[index] === " "){
newArr.push("%20");
}else{
newArr.push(oldArr[index]);
}
}
return newArr.join("");
}
思路
:创建数组,遍历链表,将链表中的元素依次从尾部插入数组中,最后反转数组。代码
:/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function printListFromTailToHead(head)
{
var array = [];
var node = head;
while(node){
array.push(node.val);
node = node.next;
}
return array.reverse();
}
思路
:前序遍历的第一个元素是根节点,中序遍历在根节点左边的是左子树,右边的是右子树,这样就可以划分中序遍历。前序遍历的左子树就是根节点之后与中序遍历左子树数量相等的所有元素,右子树就是剩下的元素,最后利用递归就可以重建二叉树了。代码
:/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function reConstructBinaryTree(pre, vin){
if(pre.length == 0 || vin.length == 0){
return null;
}else{
var rootIndex = vin.indexOf(pre[0]);
var vinLeft = vin.slice(0,rootIndex);
var vinRight = vin.slice(rootIndex+1);
var preLeft = pre.slice(1,vinLeft.length+1);
var preRight = pre.slice(vinLeft.length+1);
return {
val: pre[0],
left: reConstructBinaryTree(preLeft,vinLeft),
right: reConstructBinaryTree(preRight,vinRight)
};
}
}
思路
:栈先进后出,队列先进先出,保证顺序即可,注意pop栈为空时的处理。代码
:var inStack = [];
var outStack = [];
function push(node){
inStack.push(node);
}
function pop(){
if(outStack.length == 0){
while(inStack.length != 0){
outStack.push(inStack.pop());
}
}
return outStack.pop();
}
思路
:采用二分法,mid>high,最小元素肯定在mid右边;mid代码
:function minNumberInRotateArray(rotateArray){
let length = rotateArray.length;
if(length == 0){
return 0;
}
let low = 0;
let high = length-1;
while(low < high){
let mid = low + Math.floor((high-low)/2);
if(rotateArray[mid] > rotateArray[high]){
low = mid+1;
}else if (rotateArray[mid] < rotateArray[high]){
high = mid;
}else{
high = high-1;
}
}
return rotateArray[low];
}
思路
:先判断几种可以预料的情况,然后使用迭代法,数值从n迭代到预料数值之前,结果从预料数值之后迭代到n,最终输出结果。代码
:function Fibonacci(n){
if(n <= 0){
return 0;
}else if(n ==1 || n == 2){
return 1
}else{
let fn1 = 1;
let fn2 = 1;
while(n > 2){
fn2 = fn1 + fn2;
fn1 = fn2 - fn1;
n--;
}
return fn2;
}
}
思路
:通过找规律发现此题为斐波那契数列,即f(n) = f(n-1) + f(n-2),因此可参照上一题解法。代码
:function jumpFloor(number){
if(number <= 0){
return 0;
}else if(number == 1){
return 1;
}else if(number == 2){
return 2;
}else{
let fn1 = 1;
let fn2 = 2;
while(number > 2){
fn2 = fn1 + fn2;
fn1 = fn2 - fn1;
number--;
}
return fn2;
}
}
思路
:找规律发现f(n)=2*f(n-1)。代码
:function jumpFloorII(number){
if(number <= 0){
return 0;
}else if(number == 1){
return 1;
}else if(number == 2){
return 2;
}else{
let fn = 2;
while(number > 2){
fn *= 2;
number--;
}
return fn;
}
}
思路
:找规律发现依旧是斐波那契数列,f(n) = f(n-1) + f(n-2)。代码
:function rectCover(number){
if(number <= 0){
return 0;
}else if(number == 1){
return 1;
}else if(number == 2){
return 2;
}else{
let fn1 = 1;
let fn2 = 2;
while(number > 2){
fn2 = fn1 + fn2;
fn1 = fn2 - fn1;
number--;
}
return fn2;
}
}
思路
:如果一个整数不为0,那么这个整数至少有一位是1。如果把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话),其余所有位将不会受到影响。这个时候如果把原来的整数和减去1之后的结果做与运算,会把原整数最右边一个1变成0。那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。代码
:function NumberOf1(n){
if(n == 0){
return 0;
}else{
let count = 0;
while(n != 0){
n = n & (n - 1);
count++;
}
return count;
}
}
思路
:情况考虑全面。代码
:function Power(base, exponent){
if(exponent == 0){
return 1;
}
if(base == 0){
return 0;
}
let result = 1;
for(let i = 0;i < Math.abs(exponent);i++){
result *= base;
}
if(exponent < 0){
result=1/result;
}
return result;
}
思路
:类似冒泡排序,在前面是偶数后面是奇数的情况下交换两数位置。代码
:function reOrderArray(array){
for(let i = 0;i<array.length-1;i++){
for(let j = 0;j<array.length-i-1;j++){
if(array[j] % 2 == 0 && array[j+1] % 2 == 1){
let temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
思路
:创建数组,遍历链表,将链表的每一个元素依次插入数组中,输出第数组长度-k个元素。代码
:/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function FindKthToTail(head, k){
if (head == null || k <= 0) {
return null;
}
var arr = [];
while (head != null) {
arr.push(head);
head = head.next;
}
return arr[arr.length - k];
}
思路
:改变链表指向。代码
:/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead){
if(pHead == null){
return null;
}
var head = pHead;
var pre = null;
var next = null;
while(head != null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
思路
:比较两个链表值的大小,另当前节点等于小的结点,当前节点的下个节点通过递归小的节点的下个节点和另一个链表的当前节点比较。代码
:/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2){
if(pHead1 === null){
return pHead2;
}else if(pHead2 === null){
return pHead1;
}
let result = {};
if(pHead1.val <= pHead2.val){
result = pHead1;
result.next = Merge(pHead1.next, pHead2);
}else{
result = pHead2;
result.next = Merge(pHead1, pHead2.next);
}
return result;
}
未完待续~