珠玑编程读书笔记——<一>

我看这本书是这样的,你呢?


也是偶然发现这本书的,好多书都没能坚持看完,希望这次能坚持下去。


第一章

问题描述:

对磁盘文件中的10000000个电话号码(7位数字)进行排序。

约束:1MB的主存,磁盘空间充足,时间最多几分钟,10秒为最佳。

我想,如果在面试的时候给我这个问题,我肯定不能给出一个让面试官满意的答案

书中提供了一些方案:

一、修改使用现有的基于磁盘的合并排序。(说实话,我没有了解过此类算法,还得花些额外的时间去学习)

二、使用多通道算法,将号码表示成32位整数,1MB空间了可以存储250000个号码,那么10000000个号码则要40个通道来读取文件,第一个通道将0到249999之间任意数读如内存,并对其排序,以此类推,第四十个通道将处理9750000到9999999之间的数。使用快排。作者说:整个程序代码只20多行,比第一个方案直接少了个‘0’。不用使用中间磁盘文件。不好的一点:读入文件40次。

三、利用位图,在这之前,关于位图,我只是了解到可以用来处理数据量比较大的排序,而对其原理、实现都一点都不知道。

  文中举了一个例子,让我对位图有了基本的认识,感觉位图确实很强大。可以节省很大的空间。

   eg:利用20位的字符串来表示一个小型的非负整数集合,如{1,2,3,5,8,13},我们可以这样存储:

     01110100100001000000

那么在这个问题中,每个号码表示一个小于千万的整数,我们则可以使用一个具有千万位的字符串来表示该文件。在该字符串中,当且仅当整数i出现在文件中时,第i个位设为1.

算法如下:  直接摘自书本

  1。初始化:

         for i = [0,n)

              bit[i] = 0

  2.处理文件:

        for each i in input file

             bit[i] = 1

 3.这时排序已经完成,直接输出就好

       for i = [0,n)

          if i = 1

              write i on the output file

我相信这个算法所有人都可以很容易的理解。

简洁、明了,易读,效率高,真是个强大的算法

对于本章节,核心思想就这样了。

我想自己用代码实现这个算,运行体验一下他的效率,但是发现第二步的处理对我来说是个问题。

所以一个遗留的问题,作者说在接下来的章节会进行描述,这正好是一个很好的诱惑,诱惑我继续往下读

好了,第一章就到这,算法已经记下,就等着实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化这段代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <tr> <td></td> <td>{{books1.name}}</td> <td>{{books1.date}}</td> <td>¥{{books1.price}}</td> <td><button @click="down(books1)">-</button>{{books4.count}}<button @click="up(books1)">+</button></td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books2.name}}</td> <td>{{books2.date}}</td> <td>¥{{books2.price}}</td> <td> <button @click="down(books2)">-</button>{{books4.count}}<button @click="up(books2)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books3.name}}</td> <td>{{books3.date}}</td> <td>¥{{books3.price}}</td> <td> <button @click="down(books3)">-</button>{{books4.count}}<button @click="up(books3)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books4.name}}</td> <td>{{books4.date}}</td> <td>¥{{books4.price}}</td> <td> <button @click="down(books4)">-</button>{{books4.count}}<button @click="up(books4)">+</button> </td> <td><button @click="del">移除</button></td> </tr> </table> <div>总价: ¥{{sum}}</div> </div> <!-- js部分 --> <script> const vm = new Vue({ el: "#demo", data: { books1: { name: '《算法导论》', date: '2006-9', price: 85.00, count: 1 }, books2: { name: '《UNIX编程艺术》', date: '2006-2', price: 59.00, count: 1 }, books3: { name: '《编程》', date: '2008-10', price: 39.00, count: 1 }, books4: { name: '《代码大全》', date: '2006-3', price: 128.00, count: 1 } }, computed: { sum () { return this.books1.price * this.books1.count + this.books2.price * this.books2.count + this.books3.price * this.books3.count + this.books4.price * this.books4.count } }, methods: { down (books1) { this.books1.count = books1.count - 1; } } }) </script> </body> </html>
04-21
优化建议: 1. 使用数组来存储书籍信息,可以避免重复的代码和变量 2. 使用计算属性来计算总价,避免重复计算 3. 将操作封装成方法,并使用可复用的组件来渲染每一行 优化后的代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <<td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <book-row v-for="(book, index) in books" :book="book" :index="index" :key="index" @change-count="changeCount" @remove-book="removeBook" /> </table> <div>总价: ¥{{totalPrice}}</div> </div> <!-- js部分 --> <script> Vue.component('book-row', { props: ['book', 'index'], methods: { down() { if (this.book.count > 1) { this.book.count--; this.$emit('change-count'); } }, up() { this.book.count++; this.$emit('change-count'); }, remove() { this.$emit('remove-book', this.index); } }, template: ` <tr> <td></td> <td>{{book.name}}</td> <td>{{book.date}}</td> <td>¥{{book.price}}</td> <td> <button @click="down">-</button> {{book.count}} <button @click="up">+</button> </td> <td> <button @click="remove">移除</button> </td> </tr> ` }); const vm = new Vue({ el: "#demo", data() { return { books: [ { name: '书籍1', date: '2022-01-01', price: 30, count: 1 }, { name: '书籍2', date: '2022-01-01', price: 40, count: 1 }, { name: '书籍3', date: '2022-01-01', price: 50, count: 1 }, { name: '书籍4', date: '2022-01-01', price: 60, count: 1 } ] }; }, methods: { changeCount() { // 重新计算总价 }, removeBook(index) { this.books.splice(index, 1); // 重新计算总价 }, }, computed: { totalPrice() { return this.books.reduce((acc, cur) => acc + cur.price * cur.count, 0); } } }); </script> </body> </html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值