美文网首页
复习算法

复习算法

作者: HaomingChen1993 | 来源:发表于2017-10-22 12:23 被阅读0次

My personal study notes for learning Algorithhms 4th Edition by Robert Sedgewick, Kevin Wayne.

1. Decimal to Binary converter **use recursion***

public static int convertInt(int num){

switch (num){

case 1: return 1;

case 0: return 0;

}

int binary = num;

int bits = binary % 2;

int rests = binary / 2;

System.out.print (convertInt(rests));

return bits;

}

2. Check for balanced parentheses in an expression

private static inttypeDetector(chars) {

switch(s) {

case'{':

return1;

case'}':

return-1;

case'[':

return2;

case']':

return-2;

case'(':

return3;

case')':

return-3;

default:

return0;

}

}

public static booleanbracesCheck(String s){

intforCB =0;//{}

intforBR =0;//[]

intforBC =0;//()

for(inta =0;a < s.length();a ++ ){

if(Math.abs((typeDetector(s.charAt(a)))) ==1){

forCB =typeDetector(s.charAt(a)) + forCB;

if(forCB <0){return false;}

}else if(Math.abs((typeDetector(s.charAt(a)))) ==2){

forBR =typeDetector(s.charAt(a)) + forBR;

if(forBR <0){return false;}

}else{

forBC =typeDetector(s.charAt(a)) + forBC;

if(forBC <0){return false;}

}

}

if(forCB == forBR && forBR == forBC && forBC ==0){

return true;

}

return false;

}

3. Weighted Quick Union

The punchline of weighted quick union is to create a new array to store height of the trees. The time expenditure of the find() method is reduced to logN of base 2 from N which was linear time. 

4.(代码有bug未解决)You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)

Output:7 -> 0 -> 8

static intsum(ListNode l){

intsum =0;

intdegree =1;

do{

sum = sum + l.val* degree;

l = l.next;

degree = degree *10;

}while(l!=null);

returnsum;

}

static publicListNodeaddTwoNumbers(ListNode l1,ListNode l2) {

intsum1 =sum(l1);

intsum2 =sum(l2);

inttSum = sum1 + sum2;

ListNode L1 =newListNode(tSum %10);

ListNode S1 = L1.next;

while(tSum/10!=0){

S1.val= (tSum/10)%10;

S1 = S1.next;

tSum = tSum /10;

print(tSum);

}

returnL1;

}

public static voidmain(String args[]){

ListNode L1 =newListNode(2);

ListNode L2 =newListNode(4);

ListNode L3 =newListNode(3);

L1.next= L2;

L2.next= L3;

L3.next=null;

ListNode S1 =newListNode(5);

ListNode S2 =newListNode(6);

ListNode S3 =newListNode(4);

S1.next= S2;

S2.next= S3;

S3.next=null;

addTwoNumbers(L1,S1);

}

5.在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

public class Solution {

public boolean Find(int target, int [][] array) {

if(array==null||array.length==0||(array.length==1&&array[0].length==0)) return false;

int column = array[0].length - 1;

int row = 0;

while(true) {

if (target == array[row][column]){return true;}

if (target < array[row][column]) {

if (column - 1 < 0){return false;}

column = column - 1;

continue;

}else{

if (row + 1 == array.length){return false;}

row = row + 1;

continue;

}

}

}

}

相关文章

  • 复习算法

    My personal study notes for learning Algorithhms 4th Edit...

  • 算法复习

    1、冒泡排序 2、二分查找 3、判断回文: 解法一(切片)、 解法二(双指针) 4、基于排列构建数组 5、数组串联...

  • 今日份打卡 181/365

    技术文章最小生成树算法复习prim算法

  • 11.27

    学习脚本 复习框架 学习算法

  • 2020-02-10 周学习计划

    1、复习K近邻算法,总结到公众号; 2、复习决策树算法,总结到公众 ; 3、学习集智学园-张江老师-netlogo...

  • 算法复习笔记

    by 等流星的牧羊人写完这个这学期大概不用写了。。。。 考点 主要考到前四章,贪心证明为止(两种,数学归纳与。。。...

  • 算法导论复习

  • 算法考试复习

    引论 算法与数据结构与程序的区别算法是求解问题的过程描述:从蛮力到策略数据结构是数据的组织与存储:从杂乱无章到井然...

  • C实现排序算法

    排序是算法中的重要内容,通过复习排序可以对算法的基本思路和分析方法进行把握。利用c复习排序算法还可以同时复盘c的内...

  • 优化算法实战

    在遗传算法详解中我们介绍了爬山算法,退火算法和遗传算法的原理。在进行代码实战前让我们先复习下这几种算法。 爬山算法...

网友评论

      本文标题:复习算法

      本文链接:https://www.haomeiwen.com/subject/lbuguxtx.html