题目描述:输入几个姓名:成绩,按照冒泡排序实现排序。
import java.util.Scanner;
public class Main {
public static class User{
public String name;
public int score;
public User(String name,int score) {
this.name = name;
this.score = score;
}
public String toString() {
return name+":"+score;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] usersInfo = str.split(" ");
User[] users = new User[usersInfo.length];
for(int i=0;i<users.length;i++) {
String[] info = usersInfo[i].split(":");
users[i] = new User(info[0], Integer.valueOf(info[1]));
}
//冒泡
for(int i=0;i<users.length;i++) {
for(int j=0;j<users.length-i-1;j++) {
if(users[j].score<users[j+1].score) {
User t = users[j];
users[j] = users[j+1];
users[j+1]=t;
}
}
}
//打印
for(int i=0;i<users.length;i++) {
System.out.println(users[i].toString());
if(i!=users.length-1) {
System.out.println(" ");
}
}
}
}
3、输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
方法一:使用函数。LinkedList 中有个方法是 add(index,value),可以指定 index 位置插入 value 值,所以我们在遍历 listNode 的同时将每个遇到的值插入到 list 的 0 位置,最后输出 listNode 即可得到逆序链表。
public class Solution{
public ArrayList printListFromTailToHead(ListNode listNode){
ArrayList<Integer> list = new Arraylist<>();
while(listNode!=null){
list.add(0,listNode.val);
listNode = listNode.next;
}
return list;
}
}
方法二:使用栈。
public class Solution{
public ArrayList printListFromTailToHead(ListNode listNode){
ArrayList<Integer> list = new ArrayList<>();
Stack sta = new Stack<>();
while(listNode!=null){
sta.push(listNode.val);
listNode = listNode.next;
}
while(!sta.empty()){
list = add(sta.pop());
}
return list;
}
}
方法三:递归的使用。
public class Solution{
ArrayList<Integer> list = new ArrayList<>();
public ArrayList printListTailToHead(ListNode listNode){
if(listNode!=null){
printListTailToHead(listNode.next);
list.add(listNode.val)
}
return list;
}
}
4、重建二叉树。输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:引自这儿
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
//由于重建二叉树的过程会用到很多边界值,所以题目所给的方法的参数是不够用的
//所以,在下面重载了这个方法,每次传入前序和中序序列以及起始位置
TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
return root;
}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
//这里是判断结束的标志,由于是递归算法,我们不可能一直执行下去,所以需要结束标志
//下面这两种情况发生一个就会结束
if(startPre>endPre||startIn>endIn)
{
return null;
}
//首先找到根节点
TreeNode root=new TreeNode(pre[startPre]);
//对中序遍历进行查找根节点
for(int i=startIn;i<=endIn;i++)
{
//找到之后,分别对左子树和右子树进行递归算法,重复此步骤
if(in[i]==pre[startPre])
{
//重建二叉树的关键就是找到其中的边界值,边界值在图中已经做了描述
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,startPre+i-startIn+1,endPre,in,i+1,endIn);
break;
}
}
return root;
}
public static void main(String[] args) {
cjecs duixiang = new cjecs();//方法1:创建对象从对象中调用reConstructBinaryTree方法。
//方法二:在前面的reConstructBinaryTree中都加上static
int[] a = {1,2,4,7,3,5,6,8};
int[] b = {4,7,2,1,5,3,8,6};
System.out.println(duixiang.reConstructBinaryTree(a,b));
}
}
5、用两个栈实现队列。用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:栈A用来作入队列,栈B用来出队列,当栈B为空时,栈A全部出栈到栈B,栈B再出栈(即出队列)
package niuke;
import java.util.Stack;
public class ylgzsxdl {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int num) {
stack1.push(num);
}
public int pop() {
if(!stack2.isEmpty()) {
return stack2.pop();
}
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
return stack2.pop();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ylgzsxdl duixiang = new ylgzsxdl();
int[] a = {1,2,3};
for(int i = 0;i<a.length;i++) {
duixiang.push(i);
System.out.println(duixiang.pop());
}
}
}
6、旋转数组的最小数字。把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
思路一:因为是旋转数组,数组从后往前是递减的。所以寻找到开始增加的位置。然后将其置换。思路二:二分法,
mid = low + (high - low)/2,需要考虑三种情况:
(1)array[mid] > array[high]:
出现这种情况的array类似[3,4,5,6,0,1,2],此时最小数字一定在mid的右边,low = mid + 1
(2)array[mid] == array[high]:
出现这种情况的array类似 [1,0,1,1,1] 或者[1,1,1,0,1],此时最小数字不好判断在mid左边还是右边,这时只好一个一个试 ,
high = high - 1
(3)array[mid] < array[high]:
出现这种情况的array类似[2,2,3,4,5,6,6],此时最小数字一定就是array[mid]或者在mid的左边。因为右边必然都是递增的。high = mid
注意这里有个坑:如果待查询的范围最后只剩两个数,那么mid 一定会指向下标靠前的数字
比如 array = [4,6]
array[low] = 4 ;array[mid] = 4 ; array[high] = 6 ;
如果high = mid - 1,就会产生错误, 因此high = mid
但情形(1)中low = mid + 1就不会错误
方法一:
package niuke;
public class xzszdzxsz {
public int minNumberInRotateArray(int [] array) {
if(array.length==0) {
return 0;
}
else {
int num=0;//寻找最小数字的位置
for(int i=array.length-1;i>=0;i--) {
int j = i-1;
if(array[j]>array[i]) {
break;
}else {
num++;
}
}
int[] brr = new int[array.length+1];
int n = num+1;//lengh-1,才是最后一个数字的位置,所以与其length-n-1,不如,n=num+1;
for(int k=0;k<array.length-1;k++) {
if(n==0) {
brr[k] = array[k-num];
}
else {
brr[k]=array[array.length-n];
n--;
}
}
//return brr[0];
// for(int i=0;i<brr.length;i++) {
// System.out.println(brr[i]);
// }
return brr[0];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
xzszdzxsz duixiang = new xzszdzxsz();
//int[] a = {3,4,5,1,2};
int[] a = {6501,6828,6963,7036,7422,7674,8146,8468,8704,8717,9170,9359,9719,9895,9896,9913,9962,154,293,334,492,1323,1479,1539,1727,1870,1943,2383,2392,2996,3282,3812,3903,4465,4605,4665,4772,4828,5142,5437,5448,5668,5706,5725,6300,6335};
//System.out.println(duixiang.minNumberInRotateArray(a));
duixiang.minNumberInRotateArray(a);
}
}
方法二:
public class Solution {
public int minNumberInRotateArray(int [] array) {
int low = 0 ; int high = array.length - 1;
while(low < high){
int mid = low + (high - low) / 2;
if(array[mid] > array[high]){
low = mid + 1;
}else if(array[mid] == array[high]){
high = high - 1;
}else{
high = mid;
}
}
return array[low];
}
}
7、斐波那契数列。输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
思路:一,用递归。二,用循环。
方法一:
//用递归方法
import java.util.Scanner;
public class fbnqsl {
public int Fibonacci(int n) {
if(n<1) {
return 0;
}
else if(n==1||n==2) {
return 1;
}else {
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fbnqsl duixiang = new fbnqsl();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(duixiang.Fibonacci(n));
}
}
方法二:循环
public class fbnqsl2 {
public int Fibonacci(int n) {
int f1=0,f2=1;
int sum=2;
if(n==1) {
return 0;
}else if(n==1||n==2) {
return 1;
}else {
for(int i=3;i<=n;i++) {
sum=f1+f2;
f1 = f2;
f2 = sum;
}
return sum;
}
}
}
8、跳台阶。一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
思路:斐波那契数列问题。根据规律:f(1) = 1, f(2) = 2, f(3) = 3, f(4) = 5, 可以总结出f(n) = f(n-1) + f(n-2),
public class ttj {
public int JumpFloor(int target) {
if(target<=0) {
return 0;
}else if(target==1) {
return 1;
}else if(target==2) {
return 2;
}else {
int sum = JumpFloor(target-1)+JumpFloor(target-2);
return sum;
}
}
}
假设现在6个台阶,我们可以从第5跳一步到6,这样的话有多少种方案跳到5就有多少种方案跳到6,另外我们也可以从4跳两步跳到6,跳到4有多少种方案的话,就有多少种方案跳到6,其他的不能从3跳到6什么的啦,所以最后就是f(6) = f(5) + f(4);这样子也很好理解变态跳台阶的问题了。
9、变态跳阶梯。一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
思路:f(1) = 1,f(2) = f(2-1) + f(2-2) //f(2-2) 表示2阶一次跳2阶的次数,f(3) = f(3-1) + f(3-2) + f(3-3) f(3) = f(3-1) + f(3-2) + f(3-3) ,f(n) = f(n-1) + f(n-2) + f(n-3) + ... + f(n-(n-1)) + f(n-n) ,f(n) = 2*f(n-1)
public class Solution {
public int JumpFloorII(int target) {
if (target <= 0) {
return -1;
} else if (target == 1) {
return 1;
} else {
return 2 * JumpFloorII(target - 1);
}
}
}
10、矩形覆盖。我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
思路:既然是长条形的,那么从后向前,最后一个矩形2*2的,只有两种情况:第一种是最后是由一个2*(n-1)的矩形加上一个竖着的2*1的矩形另一种是由一个2*(n-2)的矩形,加上两个横着的2*1的矩形因此我们可以得出,第2*n个矩形的覆盖方法等于第2*(n-1)加上第2*(n-2)的方法。
public int RectCover(int target) {
if(target==0||target==1||target==2) {
return target;
}else
return RectCover(target-1)+RectCover(target-2);
}
11、二进制中1的个数。输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
思路:思路:如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。举个例子:一个二进制数1100,从右边数起第三位是处于最右边的一个1。减去1后,第三位变成0,它后面的两位0变成了1,而前面的1保持不变,因此得到的结果是1011.我们发现减1的结果是把最右边的一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算,从原来整数最右边一个1那一位开始所有位都会变成0。如1100&1011=1000.也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。
public class ejzzdygs {
public int NumberOf1(int n) {
int count=0;
while(n!=0) {
count++;
n = n&(n-1);
}
return count;
}
}
12、链表中倒数第K个结点。输入一个链表,输出该链表中倒数第k个结点。
思路:设置两个指针,
public class lbzds {
public ListNode FindKthTotail(ListNode head,int k) {
ListNode p=head,q = head;
//记录K值
int a = k;
//记录节点的个数
int count = 0;
//p先跑k-1个;
while(p!=null) {
p = p.next;
count++;
if(k<1) {
q = q.next;
}
k--;
}
//如果节点个数小于所求的倒数第K个节点,则返回空
if(count<a) {
return null;
}
return q;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
p,q;p先开始跑k-1,然后q开始跑,当p跑到头的时候,q的位置即为倒数k的位置。
13、反转链表。输入一个链表,反转链表后,输出新链表的表头。
思路:有点像滑动窗口
public class fzlbnew {
public ListNode ReverseList(ListNode head) {
ListNode pre = null;//新链表的表头
ListNode tem = null;//保存临时节点
if(head==null) {
return null;
}
while(head!=null) {
tem = head.next;
head.next = pre; //断掉原来head指向head.next的链,重新定义head指向result的新链
pre = head;//pre向前移一位 新链表的表头是pre
head = tem;//head取代原链表的head.next位置
}
return pre;
}
private static ListNode buildListNode(int[] input) {
ListNode first = null,last = null,newNode;
if(input.length>0) {
for(int i=0;i<input.length;i++) {
newNode = new ListNode(input[i]);
newNode.next = null;
if(first==null) {
first=newNode;
last = newNode;
}else {
last.next = newNode;
last = newNode;
}
}
}
return first;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fzlbnew dx = new fzlbnew();
int[] input = new int[] {1,2,3,4,5};
ListNode listNode = buildListNode(input);
System.out.println(dx.ReverseList(listNode).val);
}
}
14、输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
递归版本:
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
if(list1.val <= list2.val){
list1.next = Merge(list1.next, list2);
return list1;
}else{
list2.next = Merge(list1, list2.next);
return list2;
}
}
当时死活没想到这个版本,大神太强了。
非递归版本:
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
ListNode first = null;
ListNode last = null;
while(list1!=null && list2!=null){
if(list1.val <= list2.val){
if(first == null){
first = last = list1;
}else{
last.next = list1;
last = last.next;
}
list1 = list1.next;
}else{
if(first == null){
first = last = list2;
}else{
last.next = list2;
last = last.next;
}
list2 = list2.next;
}
}
if(list1 == null){
last.next = list2;
}else{
last.next = list1;
}
return mergeHead;
15、二叉树的镜像。操作给定的二叉树,将其变换为源二叉树的镜像。
思路一:递归方法
public void Mirror(TreeNode root) {
//设置结束条件
if(root == null) {
return;
}
if(root.left == null && root.right== null) {
return;
}
//设置临时值
TreeNode Tem = root.left;
root.left = root.right;
root.right = Tem;
//递归
if(root.left != null) {
Mirror(root.left);
}
if(root.right != null) {
Mirror(root.right);
}
}
思路二:利用栈。
public void Mirror(TreeNode root) {
if(root == null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.empty()) {
TreeNode node = stack.pop();
if(node.left != null || node.right != null) {
TreeNode nodeLeft = node.left;
TreeNode nodeRight = node.right;
node.left = nodeRight;
node.right = nodeLeft;
}
if(node.left != null) stack.push(node.left);
if(node.right != null) stack.push(node.right);
}
}
16、栈的压入弹出序列。输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)。
思路:【思路】借用一个辅助的栈,遍历压栈顺序,先讲第一个放入栈中,这里是1,然后判断栈顶元素是不是出栈顺序的第一个元素,这里是4,很显然1≠4所以我们继续压栈,直到相等以后开始出栈,出栈一个元素,则将出栈顺序向后移动一位,直到不相等,这样循环等压栈顺序遍历完成,如果辅助栈还不为空,说明弹出序列不是该栈的弹出顺序。
public class zdyrtc {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0) {
return false;
}
Stack<Integer> sta1 = new Stack();
//用于标识弹出序列的位置
int popIndex = 0;
for(int i = 0;i < pushA.length;i++) {
sta1.push(pushA[i]);
//如果栈不为空,且栈顶元素等于弹出序列
while(!sta1.empty()&&sta1.peek() == popA[popIndex]) {
//出栈
sta1.pop();
//弹出序列向后一位
popIndex++;
}
}
return sta1.empty();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}