二、解题思路
class Solution {
public static int findRepeatNumber(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int i=0;i<nums.length;i++){
if (set.add(nums[i])==false){
return nums[i];
}
}
return -1;
}
}
一、知识点
二、解题思路
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
//获取二维数组的长和宽
int length = matrix.length;
if(length==0) return false;
int width = matrix[0].length;
if(width==0) return false;
for(int i=0;i<length;i++){
if(target>=matrix[i][0] & target<=matrix[i][width-1]) {
for(int j =0 ;j<width;j++){
if(target==matrix[i][j]) return true;
}
}
}
return false;
}
}
二、解题思路
class Solution {
public String replaceSpace(String s) {
//创建一个StringBUilder,保存结果
StringBuilder result = new StringBuilder();
for(Character x : s.toCharArray()){
if(x==' ') result.append("%20");
else result.append(x);
}
return result.toString();
}
}
一、知识点
二、解题思路
class Solution {
public int[] reversePrint(ListNode head) {
LinkedList<Integer> stack = new LinkedList<Integer>();
while(head!=null){
stack.addFirst(head.val);
head=head.next;
}
int[] res = new int[stack.size()];
for(int i=0;i<res.length;i++){
res[i] = stack.removeFirst();
}
return res;
}
}
二、解题思路
使用LinkedList实现栈A、B;
通过 A栈进行入队操作,只需执行进栈操作即可;
通过B栈进行出队操作,由于要实现队列,需要将A栈中的元素出栈进去B栈,此时B中的元素为倒序;出队时只需要执行B栈的出栈操作即可;
当B中还存在元素时,直接进行出栈即可;
class CQueue {
LinkedList<Integer> A, B;
public CQueue() {
A = new LinkedList<Integer>();
B = new LinkedList<Integer>();
}
public void appendTail(int value) {
A.addLast(value);
}
public int deleteHead() {
//若B是非空,表明B中已存在倒序元素,直接进行出栈
if(!B.isEmpty()) return B.removeLast();
//若A也是空,返回-1
if(A.isEmpty()) return -1;
//将A中的元素倒序放入B中
while(!A.isEmpty()){
B.addLast(A.removeLast());
}
return B.removeLast();
}
}
二、解题思路
class Solution {
public int fib(int n) {
int a=0;
int b=1;
int sum;
for(int i=0;i<n;i++){
sum = (a+b)%1000000007;
a=b;
b=sum;
}
return a;
}
}
二、解题思路
class Solution {
public int numWays(int n) {
int a=1;
int b=2;
int sum;
for(int i=0;i<n-1;i++){
sum =(a+b)%1000000007;
a=b;
b=sum;
}
return a;
}
}
二、解题思路
方法1:遍历找旋转点
class Solution {
public int minArray(int[] numbers) {
int result=numbers[0];
for(int i=0;i<numbers.length-1;i++){
if(numbers[i]>numbers[i+1]){
result = numbers[i+1];
break;
}
}
return result;
}
}
方法二:二分查找
class Solution {
public int minArray(int[] numbers) {
int L=0;
int R=numbers.length-1;
int mid;
while(L<R){
mid = L + (R-L)/2;
if(numbers[mid]>numbers[R]) L=mid+1;
else if(numbers[mid]<numbers[R]) R=mid;
else R--;
}
return numbers[R];
}
}
二、解题思路
二、解题思路
class Solution {
int res;
boolean[][] marked;
public int movingCount(int m, int n, int k) {
marked = new boolean[m][n];
dfs(0,0,m,n,k);
return res;
}
public void dfs(int x,int y, int m, int n ,int k){
if(x>=m || y>=n || marked[x][y] || sum(x)+sum(y)>k) return;
marked[x][y] = true;
res++;
dfs(x+1,y,m,n,k);
dfs(x,y+1,m,n,k);
}
public int sum(int x){
int sum = 0;
while(x!=0){
sum+=x%10;
x=x/10;
}
return sum;
}
}
二、解题思路
思路一:动态规划(1 思路二:贪心算法(1 二、解题思路 思路一:移位法 思路二:消1法 思路三:使用 Integer.bitCount() 二、解题思路 思路一:递归 一、知识点 二、解题思路
class Solution {
public int cuttingRope(int n) {
if(n==2) return 1;
int[] dp = new int[n+1];
dp[2] = 1;
dp[3]=2;
for(int i=4;i<n+1;i++){
int max = 0;
for(int j=2; j<=i/2;j++){
int temp = Math.max(dp[j],j)*Math.max(dp[i-j],i-j);
if(temp>max) max =temp;
}
dp[i]=max;
}
return dp[n];
}
}
class Solution {
public int cuttingRope(int n) {
long res=1; //由于res*3可能超出int32的范围,例如res=1000000006,因此使用long类型
if(n==2) return 1;
if(n==3) return 2;
while(n>4){
res*=3;
res = res%1000000007;
n=n-3;
}
return (int)(res*n%1000000007); //出循环之后,n的可能取值:n = 2,3,4
}
}
13、剑指 Offer 15. 二进制中1的个数
(1)位运算
(2)Integer类的使用
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num =0;
for(int i =0 ; i< 32 ; i++){
if((n&1)==1) num++;
n=n>>1;
}
return num;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num =0;
while(n!=0){
num++;
n=n&(n-1);
}
return num;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}
14、剑指 Offer 16. 数值的整数乘方
(1)递归
(2)Java中幂运算
class Solution {
public double myPow(double x, int n) {
if(n==0) return 1.0;
if(n<0) return 1/(x*myPow(x,-n-1));
else if(n%2==1) return x*myPow(x,n-1);
return myPow(x*x,n/2);
}
}
15、剑指 Offer 17. 打印从1到最大的n位数(简单)
class Solution {
public int[] printNumbers(int n) {
int end = (int)Math.pow(10,n)-1;
int[] res = new int[end];
for(int i = 0; i< end; i++) res[i] = i+1;
return res;
}
}
16、剑指 Offer 18. 删除链表的节点(简单)
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
}
}
17、剑指 Offer 19. 正则表达式匹配(困难)
18、剑指 Offer 20. 表示数值的字符串
(1)String类的常用方法.
class Solution {
public boolean isNumber(String s) {
s = s.trim();
if(s.contains("e")){
String[] arr = s.split("e",2);
if(arr.length!=2) return false;
else return (isInt(arr[0]) || isDecimal(arr[0])) && isInt(arr[1]);
}
else if(s.contains("E")){
String[] arr = s.split("E",2);
if(arr.length!=2) return false;
else return (isInt(arr[0]) || isDecimal(arr[0])) && isInt(arr[1]);
}
else if(s.contains(".")) return isDecimal(s);
return isInt(s);
}
public boolean isDecimal(String s){
int n = s.length();
String temp = "0123456789";
if(n==0) return false;
else if(!s.contains(".")) return false;
String[] arr = s.split("\\.",2);
if(arr.length==1) return isInt(arr[0]);
else if(arr.length==2){
if(arr[0].equals("")) return isIntNumber(arr[1]);
else if(arr[0].equals("+")) return isIntNumber(arr[1]);
else if(arr[0].equals("-")) return isIntNumber(arr[1]);
else if(arr[1].equals("")) return isInt(arr[0]);
else return isInt(arr[0]) && isIntNumber(arr[1]);
}
else return false;
}
public boolean isInt(String s) {
int n = s.length();
String temp = "0123456789";
if(n==0) return false;
else if(s.charAt(0)=='+' || s.charAt(0)=='-'){
if(n==1) return false;
for(int i=1; i<n; i++){
if(!temp.contains(""+s.charAt(i))) return false;
}
return true;
}
for(int i=0; i<n; i++){
if(!temp.contains(String.valueOf(s.charAt(i)))) return false;
}
return true;
}
public boolean isIntNumber(String s) {
int n = s.length();
String temp = "0123456789";
if(n==0) return false;
for(int i=0; i<n; i++){
if(!temp.contains(String.valueOf(s.charAt(i)))) return false;
}
return true;
}
}