【剑指offer合集】

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
****************************************************************/

不够优化,可以使用二分查找

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值