《算法》第三章部分程序 part 5
▶ 书中第三章部分程序,加上自己补充的代码,包含公共符号表、集合类型
● 公共符号表,用于普通查找表的基本类
package package01; import java.util.NoSuchElementException;
import java.util.TreeMap;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class class01<Key extends Comparable<Key>, Value> implements Iterable<Key>
{
private TreeMap<Key, Value> st; public class01()
{
st = new TreeMap<Key, Value>();
} public Value get(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<get> key == null.\n");
return st.get(key);
} public void put(Key key, Value val)
{
if (key == null)
throw new IllegalArgumentException("\n<put> key == null.\n");
if (val == null)
st.remove(key);
else
st.put(key, val);
} public void delete(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<delete> key == null.\n");
st.remove(key);
} public boolean contains(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<contains> key == null.\n");
return st.containsKey(key);
} public int size()
{
return st.size();
} public boolean isEmpty()
{
return size() == 0;
} public Iterable<Key> keys()
{
return st.keySet();
} public Key min()
{
if (isEmpty())
throw new NoSuchElementException("\n<min> empty.\n");
return st.firstKey();
} public Key max()
{
if (isEmpty())
throw new NoSuchElementException("\n<max> empty.\n");
return st.lastKey();
} public Key ceiling(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<min> key == null.\n");
Key k = st.ceilingKey(key);
if (k == null)
throw new NoSuchElementException("\n<min> k == null.\n");
return k;
} public Key floor(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<min> key == null.\n");
Key k = st.floorKey(key);
if (k == null)
throw new NoSuchElementException("\n<min> k == null.\n");
return k;
} public static void main(String[] args)
{
class01<String, Integer> st = new class01<String, Integer>();
for (int i = 0; !StdIn.isEmpty(); i++)
{
String key = StdIn.readString();
st.put(key, i);
}
for (String s : st.keys())
StdOut.println(s + " " + st.get(s));
}
}
● 集合类型
package package01; import java.util.NoSuchElementException;
import java.util.Iterator;
import java.util.TreeSet;
import edu.princeton.cs.algs4.StdOut; public class class01<Key extends Comparable<Key>> implements Iterable<Key>
{
private TreeSet<Key> set; public class01()
{
set = new TreeSet<Key>();
} public class01(class01<Key> x)
{
set = new TreeSet<Key>(x.set);
} public void add(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<add> key == null.\n");
set.add(key);
} public boolean contains(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<contains> key == null.\n");
return set.contains(key);
} public void delete(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<delete> key == null.\n");
set.remove(key);
} public int size()
{
return set.size();
} public boolean isEmpty()
{
return size() == 0;
} public Iterator<Key> iterator()
{
return set.iterator();
} public Key max()
{
if (isEmpty())
throw new NoSuchElementException("\n<max> empty.\n");
return set.last();
} public Key min()
{
if (isEmpty())
throw new NoSuchElementException("\n<min> key == null.\n");
return set.first();
} public Key ceiling(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<ceiling> key == null.\n");
Key k = set.ceiling(key);
if (k == null)
throw new NoSuchElementException("\n<ceiling> k == null.\n");
return k;
} public Key floor(Key key)
{
if (key == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
Key k = set.floor(key);
if (k == null)
throw new NoSuchElementException("\n<floor> k == null.\n");
return k;
} public class01<Key> union(class01<Key> that)
{
if (that == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
class01<Key> c = new class01<Key>();
for (Key x : this)
c.add(x);
for (Key x : that)
c.add(x);
return c;
} public class01<Key> intersects(class01<Key> that)
{
if (that == null)
throw new IllegalArgumentException("\n<floor> key == null.\n");
class01<Key> c = new class01<Key>();
if (size() < that.size()) // 遍历较小的集合,去较大的集合中匹配,无所谓?
{
for (Key x : this)
{
if (that.contains(x))
c.add(x);
}
}
else
{
for (Key x : that)
{
if (contains(x))
c.add(x);
}
}
return c;
} public boolean equals(Object other)
{
if (other == this)
return true;
if (other == null)
return false;
if (other.getClass() != getClass())
return false;
class01 that = (class01) other;
return set.equals(that.set);
} @Override
public int hashCode()
{
throw new UnsupportedOperationException("\n<hashCode> hashCode() not supported,\n");
} @Override
public String toString() // 把集合的元素放进大括号中列出
{
String s = set.toString();
return "{ " + s.substring(1, s.length() - 1) + " }";
} public static void main(String[] args)
{
class01<String> set = new class01<String>();
StdOut.println("set = " + set); // 输出空集合 set.add("www.cs.princeton.edu"); // 插入一些元素用于测试方法
set.add("www.cs.princeton.edu");
set.add("www.princeton.edu");
set.add("www.math.princeton.edu");
set.add("www.yale.edu");
set.add("www.amazon.com");
set.add("www.simpsons.com");
set.add("www.stanford.edu");
set.add("www.google.com");
set.add("www.ibm.com");
set.add("www.apple.com");
set.add("www.slashdot.com");
set.add("www.whitehouse.gov");
set.add("www.espn.com");
set.add("www.snopes.com");
set.add("www.movies.com");
set.add("www.cnn.com");
set.add("www.iitb.ac.in"); StdOut.println(set.contains("www.cs.princeton.edu"));
StdOut.println(!set.contains("www.harvardsucks.com"));
StdOut.println();
StdOut.println("ceiling(www.simpsonr.com) = " + set.ceiling("www.simpsonr.com"));
StdOut.println("ceiling(www.simpsons.com) = " + set.ceiling("www.simpsons.com"));
StdOut.println("floor(www.simpsonr.com) = " + set.floor("www.simpsonr.com"));
StdOut.println("floor(www.simpsons.com) = " + set.floor("www.simpsons.com"));
StdOut.println();
StdOut.println("set = " + set);
StdOut.println();
for (String s : set) // 直接列出表中元素
StdOut.println(s);
StdOut.println();
class01<String> set2 = new class01<String>(set);
StdOut.println(set.equals(set2));
}
}
《算法》第三章部分程序 part 5的更多相关文章
- 《算法》第三章部分程序 part 6
▶ 书中第三章部分程序,加上自己补充的代码,包含双向索引表.文建索引.稀疏向量类型 ● 双向索引表 package package01; import edu.princeton.cs.algs4.S ...
- 《算法》第三章部分程序 part 4
▶ 书中第三章部分程序,加上自己补充的代码,包括散列表.线性探查表 ● 散列表 package package01; import edu.princeton.cs.algs4.Queue; impo ...
- 《算法》第三章部分程序 part 3
▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...
- 《算法》第三章部分程序 part 2
▶ 书中第三章部分程序,加上自己补充的代码,平衡二叉搜索树 ● 平衡二叉搜索树 package package01; import java.util.NoSuchElementException; ...
- 《算法》第三章部分程序 part 1
▶ 书中第三章部分程序,加上自己补充的代码,包括单词频率统计,(单链表)顺序查找表,二分查找表 ● 单词频率统计 package package01; import edu.princeton.cs. ...
- 《算法》第二章部分程序 part 3
▶ 书中第二章部分程序,加上自己补充的代码,包括各种优化的快排 package package01; import edu.princeton.cs.algs4.In; import edu.prin ...
- 《算法》第一章部分程序 part 1
▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...
- 《算法》第二章部分程序 part 5
▶ 书中第二章部分程序,加上自己补充的代码,包括利用优先队列进行多路归并和堆排序 ● 利用优先队列进行多路归并 package package01; import edu.princeton.cs.a ...
- 《算法》第二章部分程序 part 4
▶ 书中第二章部分程序,加上自己补充的代码,包括优先队列和索引优先队列 ● 优先队列 package package01; import java.util.Comparator; import ja ...
随机推荐
- [置顶] Web用户的身份验证及WebApi权限验证流程的设计和实现 (不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!!)
转发 http://blog.csdn.net/besley/article/details/8516894 不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!! [ ...
- NSIS 制作简单安装包
;replace NSIS/Contrib/zip2exe/Modern.nsh;NSI format: Asni;http://nsis.sourceforge.net/ ;UAC级别Request ...
- php访问SQLserver时加载的dll
php_sqlsrv_55_ts.dll 线程t安全sphp_sqlsrv_55_nts.dll 非n线程t安全s
- 容易掉坑的地方The value for the useBean class attribute XXX is invalid
1.在编译 JSP 时,指定的 Bean 类没找到(没找到原因有多种所以复杂就在这里,比如文件名是否写错,包名是否写错,<jsp:useBean class="路径"> ...
- <亲测>阿里云centos7安装redis
安装redis yum install redis 启动redis systemctl start redis.service 设置redis开机启动 systemctl enable redis.s ...
- LINUX 内存使用情况
# free 显示结果如下: Mem:表示物理内存统计 total 内存总数 8057964KBused 已使用的内存 7852484KBfree 空闲的内存数 205480KBshared 当前已经 ...
- C++进阶--模板及关键字typename
//############################################################################ /* * 模板介绍 */ //函数模板 t ...
- TCP/IP学习20180627-数据链路层-ethernet
ifconfig :查看主機支持的網絡協議eth0:以太網接口lo:loopback接口 以太网(Ether-net)的定是指数字设备公司( Digital Equipment Corp.).英特尔公 ...
- P2837晚餐队列安排
传送 特写此篇,纪念不用dp做dp题 洛谷说这是个dp,但我不信(其实就是不会dp),因此我们考虑用另一种思路.修改后的队列每一个 数a[i]一定满足a[i]<=a[i+1],那修改后的顺序就是 ...
- 在 Vue 项目中引入 tinymce 富文本编辑器
项目中原本使用的富文本编辑器是 wangEditor,这是一个很轻量.简洁编辑器 但是公司的业务升级,想要一个功能更全面的编辑器,我找了好久,目前常见的编辑器有这些: UEditor:百度前端的开源项 ...