3.二维数组的查找
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main2
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (sc.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) sc.nval;
sc.nextToken();
int n = (int) sc.nval;
sc.nextToken();
int t = (int) sc.nval;
int[][] a = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
sc.nextToken();
a[i][j] = (int) sc.nval;
}
if (foo(t, a))
System.out.println("Yes");
else
System.out.println("No");
}
}
public static boolean foo(int t, int[][] a)
{
int m=a.length;
int n=a[0].length;
if (t < a[0][0] || t > a[m - 1][n - 1])
return false;
else {
int row=0;
int col=n-1;
while (row<m&&col>=0) {
if (t < a[row][col]) {
col--;
} else if (t > a[row][col]) {
row++;
} else
return true;
}
}
return false;
}
}
4.替换空格
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().replaceAll(" ", "%20");
System.out.println(str);
}
}
注意next及nextInt等都不会读到换行符,所以如果要混用,在某行要先调用nextLine跳过换行符。
不使用库函数,采用字符数组
import java.util.Scanner;
public class Main2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char[] str = sc.nextLine().toCharArray();
int count=0;
for(int i=0;i<str.length;i++)
if(str[i]==' ')
count++;
char[] c=new char[str.length+count*2];
int p=str.length-1;
int q=str.length+count*2-1;
while(p>=0)
{
if(str[p]!=' ')
c[q--]=str[p--];
else
{
c[q--]='0';
c[q--]='2';
c[q--]='%';
p--;
}
}
System.out.println(c);
}
}
5.从尾到头打印链表
保存到堆里
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Deque;
import java.util.LinkedList;
public class Main
{
public static void main(String[] args) throws IOException
{
Deque<Integer> dq=new LinkedList<Integer>();
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while(sc.nextToken() != StreamTokenizer.TT_EOF)
{
int a= (int) sc.nval;
if(a==-1)
break;
else
dq.push(a);
}
while(!dq.isEmpty())
System.out.println(dq.pop());
}
}
注意打印若改为for循环使用迭代器,也是可以的
虽然迭代器只支持正向,但push是调用addFirst,所以打印顺序为出栈顺序
7.用两个栈实现队列
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
public class Main2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Deque<Integer> stack1 = new LinkedList<Integer>();
Deque<Integer> stack2 = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
String s = sc.next();
if (s.equals("PUSH")) {
stack1.push(sc.nextInt());
} else if (s.equals("POP")) {
if (!stack2.isEmpty())
System.out.println(stack2.pop());
else if (stack1.isEmpty())
System.out.println(-1);
else {
while (stack1.size() > 1)
stack2.push(stack1.pop());
System.out.println(stack1.pop());
}
}
}
}
}
注意优化,出stack1后不用再回去
8.旋转数组的最小数字
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main
{
public static void main(String[] args) throws IOException
{
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while(sc.nextToken() != StreamTokenizer.TT_EOF)
{
int n=(int) sc.nval;
int[] a=new int[n];
for(int i=0;i<n;i++)
{
sc.nextToken();
a[i]=(int) sc.nval;
}
int i=0;
while(i<n-1&&a[i+1]>=a[i])
i++;
if(i==n-1)
System.out.println(a[0]);
else
System.out.println(a[i+1]);
}
}
}
/**************************************************************
Problem: 1386
User: chicc999
Language: Java
Result: Accepted
Time:1910 ms
Memory:28868 kb
****************************************************************/
不够优化,可以使用二分查找