算法(Algorithms)第4版 练习 1.3.21
2024-08-30 02:55:29
方法实现:
//1.3.21
/**
* find if some node in the list has key as its item field
*
* @param list the linked list of T
* @param key the T key
*
* @return {@code true} some node exists.
* {@code false} no node exist
*/
public static <T> boolean find(LinkedList<T> list, T key) { for(T s : list) {
if(s.equals(key))
return true;
} return false;
}
测试用例:
package com.qiusongde.linkedlist; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Exercise1321 { public static void main(String[] args) { LinkedList<String> list = new LinkedList<String>(); while(!StdIn.isEmpty()) {
String s = StdIn.readString();
list.insertAtBeginning(s);
StdOut.println("insertAtBeginning success: " + s);
StdOut.println(list);
} String key = "qiu";
StdOut.println("find " + key + " :" + LinkedList.find(list, key)); key = "not";
StdOut.println("find " + key + " :" + LinkedList.find(list, key)); } }
输入数据:
to
be
or
not
to
输出结果:
insertAtBeginning success: to
to
insertAtBeginning success: be
be to
insertAtBeginning success: or
or be to
insertAtBeginning success: not
not or be to
insertAtBeginning success: to
to not or be to
find qiu :false
find not :true
最新文章
- NK3C:关于svg文件使用
- 使用Vs2012开发Metro时在另一台win8平板上调试的步骤
- Windows下好用到必须开机自启的小工具
- osharpV3数据库初始化
- windows程序是如何开始执行的??
- [原创]java WEB学习笔记56:Struts2学习之路---Struts 版本的 登录 demo
- HackerRank ";Lucky Numbers";
- js控制ul的显示隐藏,对象的有效范围
- c++继承中的内存布局
- Delphi中编辑word
- Socket(TCP)客户端请求和服务端监听和链接基础(附例子)
- Find Peak Element 解答
- vim 括号自动补全
- SpringBoot系列: 单元测试2
- kindle完结书单
- C++面试笔记(2)
- 06-HTML-表格标签
- vue内置组件 transition 和 keep-alive 使用
- Flink--Split和select
- geeksforgeeks-Array-Rotation and deletion
热门文章
- from: Maven实战(九)——打包的技巧
- jQuery ajax 获取信息展示在“下拉列表”中
- LM339组成的双限比较器
- slam cartographer 学习
- 求解复数组 中模较大的N个数
- Chrome自带恐龙小游戏的源码研究(六)
- Python结合NC.exe 实现模拟登录&;批量填表
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- 畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
- 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)