- 题目描述
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
public class Main11 {
public int NumberOf1(int n) {
int count=0;
if(n<0){
n= n & 0x7FFFFFFF;
count++;
}
while(n!=0){
count += n & 1;
n = n >> 1;
}
return count;
}
}
12.题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
public class Main12 {
public double Power(double base, int exponent) {
double db = 1;
if(exponent>0){
for (int i = 0; i < exponent; i++) {
db = db * base;
}
}else if(exponent<0){
if(base==0){
System.out.println("分母不能为0");
}else{
for (int i = 0; i < -exponent; i++) {
db = db * base;
}
db=1/db;
}
}else{
return 1;
}
return db;
}
}
- 题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,
使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,
并保证奇数和奇数,偶数和偶数之间的相对位置不变。
public class Main13 {
public static void reOrderArray(int [] array) {
int count = 0;
for(int i=0; i<array.length; i++){
if(array[i]%2==1){
int temp = array[i];
for(int j=i; j>count; j--){
array[j] = array[j-1];
}
array[count] = temp;
count++;
}
}
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
reOrderArray(array);
for(int i=0; i<array.length; i++){
System.out.print(array[i]+",");
}
}
}
14.题目描述
输入一个链表,输出该链表中倒数第k个结点。
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public class Main14 {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null){
return head;
}
ListNode n1=head,n2 = head;
int i=1;
while(i++!=k){
n2=n2.next;
}
while(n2.next!=null){
n1=n1.next;
n2=n2.next;
}
return n1;
}
}
15.题目描述
输入一个链表,反转链表后,输出新链表的表头。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Main15 {
public static ListNode way(ListNode root){
if(root==null){
return null;
}
Stack<ListNode> stack = new Stack<>();
while(root!=null){
stack.push(root);
root=root.next;
}
ListNode head = null;
if(!stack.empty()){
head = root = stack.pop();
}
while(!stack.empty()){
head.next = stack.pop();
head = head.next;
}
head.next=null;
return root;
}
}
16.题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Main16 {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
ListNode head = null;
ListNode end = null;
if(list1.val <= list2.val){
if(head == null){
head = end = list1;
}
end.next = Merge(list1.next, list2);
}else{
if(head == null){
head = end = list2;
}
end.next = Merge(list1, list2.next);
}
return head;
}
}
17.题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Main17 {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
Boolean flag = false;
if(root1 != null && root2 != null){
if(root1.val == root2.val){
flag = ways(root1, root2);
}
if (!flag){
flag = HasSubtree(root1.left, root2);
}
if (!flag){
flag = HasSubtree(root1.right, root2);
}
}
return flag;
}
public static Boolean ways(TreeNode root1,TreeNode root2){
if(root2 == null){
return true;
}
if(root1 == null && root2 != null){
return false;
}
if(root1.val != root2.val){
return false;
}
return ways(root1.left, root2.left) && ways(root1.right, root2.right);
}
18.题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
public class Main18 {
//递归
/* public void Mirror(TreeNode root) {
if(root != null){
TreeNode node = root.left;
root.left = root.right;
root.right = node;
}
Mirror(root.left);
Mirror(root.right);
}*/
//非递归
public void Mirror(TreeNode root) {
if(root==null){
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
TreeNode treeNode = node.left;
node.left = node.right;
node.right = treeNode;
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}
}
}
19.题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
public class Main19 {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> list = new ArrayList<>();
if(matrix==null||matrix.length==0){
return list;
}
ways(matrix, 0, 0, matrix.length-1, matrix[0].length-1, list);
return list;
}
public static void ways(int [][] matrix, int rowStart, int colStart, int rowEnd, int colEnd, ArrayList<Integer> list){
if(rowStart<rowEnd && colStart<colEnd){
for(int i=colStart; i<=colEnd; i++){
list.add(matrix[rowStart][i]);
}
for(int i=rowStart+1; i<=rowEnd; i++){
list.add(matrix[i][colEnd]);
}
for(int i=colEnd-1; i>=colStart; i--){
list.add(matrix[rowEnd][i]);
}
for(int i=rowEnd-1; i>rowStart; i--){
list.add(matrix[i][colStart]);
}
ways(matrix, rowStart+1, colStart+1, rowEnd-1, colEnd-1, list);
}else if(rowStart==rowEnd && colStart<colEnd){
for(int i=colStart; i<=colEnd; i++){
list.add(matrix[rowStart][i]);
}
}else if(rowStart<rowEnd && colStart==colEnd){
for(int i=rowStart; i<=rowEnd; i++){
list.add(matrix[i][colStart]);
}
}else if(rowStart==rowEnd && colStart==colEnd){
list.add(matrix[rowStart][colStart]);
}
}
}
20.题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
public class Main20 {
Stack<Integer> stack = new Stack<>();
public void push(int node) {
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
int min = stack.peek();
Iterator<Integer> iterator = stack.iterator();
while(iterator.hasNext()){
int temp = iterator.next();
if(temp<min){
min = temp;
}
}
return min;
}
}