「算法java查询」java查询方法
今天给各位分享算法java查询的知识,其中也会对java查询方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、试编写一个算法从检查一个Java语言中的大括号方括号小括号是否配对,若能够全?
- 2、Java用查找算法的一段代码如下: 其中boolean A=false; if(name.equals(arr[i])) 麻烦解释一下 尽量直白
- 3、用JAVA编写一个简单的 查询算法!谢谢大哥大姐了
- 4、请教:用JAVA编一个基本查找算法效率比较的程序。
- 5、java关键字查询算法
试编写一个算法从检查一个Java语言中的大括号方括号小括号是否配对,若能够全?
要编写用于检查 Java 程序中的大括号、方括号和圆括号是否正确匹配的算法,可以使用堆栈数据结构来跟踪代码中遇到的左大括号。例如,您可以使用以下步骤:
初始化空堆栈。
循环遍历 Java 程序中的字符,从第一个字符开始。
如果当前字符是左大括号(即、、或),则将其推送到堆栈上。{[(
如果当前字符是右大括号(即、或),则从堆栈中弹出顶部元素。如果弹出的元素与当前右大括号不匹配,则返回(指示大括号未正确匹配)。}])False
重复步骤 3 和 4,直到到达 Java 程序的末尾。
如果堆栈为空,则返回(指示大括号正确匹配)。否则,返回(表示大括号未正确匹配)。TrueFalse
此算法使用堆栈来跟踪 Java 程序中遇到的左大括号,并通过将右大括号与从堆栈中弹出的相应左大括号进行比较来检查是否匹配。此方法可以处理大括号的任意嵌套结构,并将正确识别 Java 程序中的大括号是否正确匹配。
请注意,这只是一个示例算法,可能还有其他方法可以解决此问题。您可以尝试不同的方法来找到最适合您的解决方案。
Java用查找算法的一段代码如下: 其中boolean A=false; if(name.equals(arr[i])) 麻烦解释一下 尽量直白
数组从第一个开始比较,完全相同(当前数组值和输入值一模一样)A就赋值为true;不一样A的值不变
用JAVA编写一个简单的 查询算法!谢谢大哥大姐了
import java.io.*;
public class Test
{
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
System.out.println("请输入要查询的单词");
String s=br.readLine();
int count=0;
int m=0;
int begin=-1;
int end=-1;
while(true)
{
if(s.length()==1)
{
begin=str.indexOf(s);
if(m==0)
{
System.out.println("第一次出现在"+begin+"字节处");
}
m++;
end=begin;
}
else
{
begin=str.indexOf(s.substring(0,1));
if(m==0)
{
System.out.println("第一次出现在"+begin+"字节处");
}
end=str.indexOf(s.substring(s.length()-1));
}
if(begin==-1||end==-1)
{
break;
}
if(s.equals(str.subSequence(begin, end+1)))
{
count++;
str=str.substring(end+1);
}
else
{
str=str.substring(end+1);
}
}
System.out.println("单词"+s+"出现了"+count+"次");
}
}
请教:用JAVA编一个基本查找算法效率比较的程序。
script
Array.prototype.swap = function(i, j)
{
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}
Array.prototype.bubbleSort = function()
{
for (var i = this.length - 1; i 0; --i)
{
for (var j = 0; j i; ++j)
{
if (this[j] this[j + 1]) this.swap(j, j + 1);
}
}
}
Array.prototype.selectionSort = function()
{
for (var i = 0; i this.length; ++i)
{
var index = i;
for (var j = i + 1; j this.length; ++j)
{
if (this[j] this[index]) index = j;
}
this.swap(i, index);
}
}
Array.prototype.insertionSort = function()
{
for (var i = 1; i this.length; ++i)
{
var j = i, value = this[i];
while (j 0 this[j - 1] value)
{
this[j] = this[j - 1];
--j;
}
this[j] = value;
}
}
Array.prototype.shellSort = function()
{
for (var step = this.length 1; step 0; step = 1)
{
for (var i = 0; i step; ++i)
{
for (var j = i + step; j this.length; j += step)
{
var k = j, value = this[j];
while (k = step this[k - step] value)
{
this[k] = this[k - step];
k -= step;
}
this[k] = value;
}
}
}
}
Array.prototype.quickSort = function(s, e)
{
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (s = e) return;
this.swap((s + e) 1, e);
var index = s - 1;
for (var i = s; i = e; ++i)
{
if (this[i] = this[e]) this.swap(i, ++index);
}
this.quickSort(s, index - 1);
this.quickSort(index + 1, e);
}
Array.prototype.stackQuickSort = function()
{
var stack = [0, this.length - 1];
while (stack.length 0)
{
var e = stack.pop(), s = stack.pop();
if (s = e) continue;
this.swap((s + e) 1, e);
var index = s - 1;
for (var i = s; i = e; ++i)
{
if (this[i] = this[e]) this.swap(i, ++index);
}
stack.push(s, index - 1, index + 1, e);
}
}
Array.prototype.mergeSort = function(s, e, b)
{
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (b == null) b = new Array(this.length);
if (s = e) return;
var m = (s + e) 1;
this.mergeSort(s, m, b);
this.mergeSort(m + 1, e, b);
for (var i = s, j = s, k = m + 1; i = e; ++i)
{
b[i] = this[(k e || j = m this[j] this[k]) ? j++ : k++];
}
for (var i = s; i = e; ++i) this[i] = b[i];
}
Array.prototype.heapSort = function()
{
for (var i = 1; i this.length; ++i)
{
for (var j = i, k = (j - 1) 1; k = 0; j = k, k = (k - 1) 1)
{
if (this[k] = this[j]) break;
this.swap(j, k);
}
}
for (var i = this.length - 1; i 0; --i)
{
this.swap(0, i);
for (var j = 0, k = (j + 1) 1; k = i; j = k, k = (k + 1) 1)
{
if (k == i || this[k] this[k - 1]) --k;
if (this[k] = this[j]) break;
this.swap(j, k);
}
}
}
function generate()
{
var max = parseInt(txtMax.value), count = parseInt(txtCount.value);
if (isNaN(max) || isNaN(count))
{
alert("个数和最大值必须是一个整数");
return;
}
var array = [];
for (var i = 0; i count; ++i) array.push(Math.round(Math.random() * max));
txtInput.value = array.join("\n");
txtOutput.value = "";
}
function demo(type)
{
var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n");
for (var i = 0; i array.length; ++i) array[i] = parseInt(array[i]);
var t1 = new Date();
eval("array." + type + "Sort()");
var t2 = new Date();
lblTime.innerText = t2.valueOf() - t1.valueOf();
txtOutput.value = array.join("\n");
}
/script
body onload=generate()
table style="width:100%;height:100%;font-size:12px;font-family:宋体"
tr
td align=right
textarea id=txtInput readonly style="width:100px;height:100%"/textarea
/td
td width=150 align=center
随机数个数input id=txtCount value=500 style="width:50px"brbr
最大随机数input id=txtMax value=1000 style="width:50px"brbr
button onclick=generate()重新生成/buttonbrbrbrbr
耗时(毫秒):label id=lblTime/labelbrbrbrbr
button onclick=demo("bubble")冒泡排序/buttonbrbr
button onclick=demo("selection")选择排序/buttonbrbr
button onclick=demo("insertion")插入排序/buttonbrbr
button onclick=demo("shell")谢尔排序/buttonbrbr
button onclick=demo("quick")快速排序(递归)/buttonbrbr
button onclick=demo("stackQuick")快速排序(堆栈)/buttonbrbr
button onclick=demo("merge")归并排序/buttonbrbr
button onclick=demo("heap")堆排序/buttonbrbr
/td
td align=left
textarea id=txtOutput readonly style="width:100px;height:100%"/textarea
/td
/tr
/table
/body
这个代码是放在DREAMWEAVER head/head标签里面
java关键字查询算法
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class search
{
//查找方法,参数,文件绝对路径,查找关键字
public static boolean search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
//int i = 1;
while((s = br.readLine()) != null)
{
if(s.indexOf(key) != -1)
{
return true;
}
}
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","l2"));
}
}
修改了下,加两个变量,可以指出查找的位置。
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
public class search
{
//查找方法,参数,文件绝对路径,查找关键字
public static String search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
int i = 1;
int m = 0;
while((s = br.readLine()) != null)
{
if((m = s.indexOf(key)) != -1)
{
return "第"+i+"段,第"+m+"处";
}
i++;
}
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","asd"));
}
}
这个,查汉字是没有问题的。
另外,你要全文检索的话,indexOf()还有个方法,indexOf(int start,String key),指定开始查找的位置跟关键字,你查到一处后,将这个数值加1,做为继续查找的开始位置就可以了。
算法java查询的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java查询方法、算法java查询的信息别忘了在本站进行查找喔。
发布于:2022-12-24,除非注明,否则均为
原创文章,转载请注明出处。