- 题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public class Solution {
public static boolean Find(int target, int [][] array) {
for (int i=0;i<array.length;i++)//java可直接得到数组的长度
for (int j=0;j<array[0].length;j++)
{
if (array[i][j]==target)
return true;
}
return false;
}
}
- 题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
public static String replaceSpace(StringBuffer str) {
String NewStr= str.toString();
return NewStr.replace(" ","%20");//String自带有替换函数replace
}
}
- 题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
public class Solution {
public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Deque<Integer> deque = new ArrayDeque<Integer>();//初始化双端队列
while(listNode!=null)
{
deque.addFirst(listNode.val);//从头部添加
listNode=listNode.next;
}
ArrayList<Integer> a=new ArrayList<Integer>();//初始化动态数组
while(deque.peek()!=null)//peek取首元素
{
a.add(deque.peek());
deque.pollFirst();//删除首元素,当完全删除完,会peek到null
}
return a;
}
}
- 题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
import java.util.Stack;
public class Solution {
static Stack<Integer> stack1 = new Stack<Integer>();
static Stack<Integer> stack2 = new Stack<Integer>();
public static void push(int node) {
stack1.push(node);
}
public static int pop() {
if(!stack2.empty())//判断是否空栈
{
return stack2.pop();//peek获取第一个值,但不出栈
}
else
{
while (!stack1.empty())
{
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
}
- 题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length==0)
return 0;
int min=array[0];
for(int k:array)
{
if(k<min)
return k;
else
min=k;
}
return 0;
}
}
- 题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
public class Solution {
public int Fibonacci(int n) {
if (n==0)
return 0;
else if (n==1)
return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
- 题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
String s= in.nextLine();
int a=s.lastIndexOf(" ");
System.out.println(s.length()-a-1);
}
}
- 题目描述
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入
ABCDEF
A
输出
1
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
String s= in.nextLine();
String com= in.nextLine();
int i=0,count=0,k;
com=com.toLowerCase();
s=s.toLowerCase();
while((k=s.indexOf(com,i))!=-1)
{
i=k+1;
count++;
}
System.out.println(count);
}
}
- 题目描述
明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
样例输入解释:
样例有两组测试
第一组是3个数字,分别是:2,2,1。
第二组是11个数字,分别是:10,20,40,32,67,40,20,89,300,400,15。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
ArrayList<Integer> arr= new ArrayList();
int length = 0,type=1;//type为状态位,0表示读取数组,1表示读取长度
while (in.hasNextInt())
{
if(type==1)//读取长度
{
length=in.nextInt();
type=0;
}
else if(length!=0)//读取数组
{
int temp=in.nextInt();
if(!arr.contains(temp))
{
arr.add(temp);
}
length --;
}
else if(length==0)//输出,然后设为读取长度
{
Collections.sort(arr);//排序
for (Integer i:arr)
{
System.out.println(i);
}
arr.clear();
type=1;
}
}
//输出最后一轮
Collections.sort(arr);
for (Integer i:arr)
{
System.out.println(i);
}
}
}