二进制中1的个数
描述
输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。
示例1
输入:
10
返回值:
2
思路:n&(n-1)会把倒数第一个1直接置0,有多少次这种操作,就有多少个1
public class Solution {
public int NumberOf1(int n) {
int count = 0;
while(n!=0){
count++;
n = n&(n-1);
}
return count;
}
}
数值的整数次方
描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
保证base和exponent不同时为0。不得使用库函数,同时不需要考虑大数问题,也不用考虑小数点后面0的位数。
示例1
输入:
2.00000,3
返回值:
8.00000
思路:对exponent分类
public class Solution {
public double Power(double base, int exponent) {
if(exponent==0){
return 1;
}else if(exponent==1){
return base;
}else if(exponent>1){
double sum = 1;
for(int i=1;i<=exponent;i++){
sum*=base;
}
return sum;
}else{
double sum=1;
for(int i=1;i<=-exponent;i++){
sum*=base;
}
return 1/sum;
}
}
}
调整数组顺序使奇数位于偶数前面
描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
示例
输入:
[2,4,6,5,7]
返回值:
[5,7,2,4,6]
思路:冒泡排序,每次扔一个奇数到前面
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
public int[] reOrderArray (int[] array) {
// write code here
//思路:冒泡排序,遇到一个奇数就往前放,并使后续数字移动
int index = -1;
for(int i = 0;i<array.length;i++){
if(array[i]%2==1){
index+=1;
int tmp = array[i];
for(int j = i;j>index;j--){
array[j]=array[j-1];
}
array[index]=tmp;
}
}
return array;
}
}
链表中倒数第k个结点
描述
输入一个链表,输出该链表中倒数第k个结点。
如果该链表长度小于k,请返回空。
示例1
输入:
{1,2,3,4,5},1
返回值:
{5}
思路:倒数k—>正数n-k+1。fast—>k步,slow开始走,fast走到空指针,意味着slow走了n-k+1
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode FindKthToTail (ListNode pHead, int k) {
// write code here
//倒数k--->正数n-k+1。fast--->k步,slow开始走,fast走到空指针,意味着slow走了n-k+1
ListNode fast = pHead;
ListNode slow = pHead;
for(int i = 0;i<k;i++){
if(fast==null){
return null;
}
fast=fast.next;
}
while(fast!=null){
fast=fast.next;
slow=slow.next;
}
return slow;
}
}
反转链表
描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入:
{1,2,3}
返回值:
{3,2,1}
思路:画图,改指向
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;
while(cur!=null){
ListNode next = cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
}
合并两个排序的链表
描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
示例1
输入:
{1,3,5},{2,4,6}
返回值:
{1,2,3,4,5,6}
思路:一个指向头,一个移动指针
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode head = new ListNode(0);
ListNode movenode = head;
while(list1!=null&&list2!=null){
if(list1.val>list2.val){
movenode.next = list2;
list2=list2.next;
}else{
movenode.next = list1;
list1=list1.next;
}
movenode = movenode.next;
}
if(list1!=null){
movenode.next = list1;
}
if(list2!=null){
movenode.next = list2;
}
return head.next;
}
}
树的子结构
描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
示例1
输入:
{8,8,#,9,#,2,#,5},{8,9,#,2}
返回值:
true
思路:a与b是否成立,a.left与b是否成立,a.right与b是否成立
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null||root2==null){
return false;
}
return DoseTree1HasTree2(root1,root2)||HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
}
public static boolean DoseTree1HasTree2(TreeNode tree1,TreeNode tree2){
if(tree2==null){
return true;
}
if(tree1==null){
return false;
}
if(tree1.val!=tree2.val){
return false;
}
return DoseTree1HasTree2(tree1.left,tree2.left)&&DoseTree1HasTree2(tree1.right,tree2.right);
}
}
二叉树的镜像
描述
操作给定的二叉树,将其变换为源二叉树的镜像。
比如: 源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
示例1
输入:
{8,6,10,5,7,9,11}
返回值:
{8,10,6,11,9,7,5}
思路:递归
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
TreeNode tmp;
if(pRoot==null){return pRoot;}
tmp = pRoot.right;
pRoot.right = pRoot.left;
pRoot.left = tmp;
Mirror(pRoot.right);
Mirror(pRoot.left);
return pRoot;
}
}
顺时针打印矩阵
描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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.
示例1
输入:
[[1,2],[3,4]]
返回值:
[1,2,4,3]
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> list = new ArrayList<Integer>();
int up = 0;
int down = matrix.length-1;
int left = 0;
int right = matrix[0].length-1;
while(true){
for(int col = left; col<=right; col++){
list.add(matrix[up][col]);
}
up++;
if(up>down){
break;
}
for(int row = up; row<=down; row++){
list.add(matrix[row][right]);
}
right--;
if(left>right){
break;
}
for(int col = right; col>=left; col--){
list.add(matrix[down][col]);
}
down--;
if(up>down){
break;
}
for(int row = down; row>=up; row--){
list.add(matrix[row][left]);
}
left++;
if(right<left){
break;
}
}
return list;
}
}
包含min函数的栈
描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
import java.util.Stack;
public class Solution {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> min = new Stack<Integer>();
public void push(int node) {
stack.push(node);
if(min.isEmpty()){
min.push(node);
}else{
if(min.peek()>node){
min.push(node);
}else{
min.push(min.peek());
}
}
}
public void pop() {
min.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return min.peek();
}
}