随笔分类 - 剑指Offer
一共就这么点题,刷透刷透刷透它!!!!
摘要:class Solution { public double myPow(double x, int n) { if(x == 1) return 1; if(x == -1) return n%2==0?1:-1; if(n<-1000) return 0; if(n<0) return 1.0/
阅读全文
摘要:public int hammingWeight(int n) { //先将n转成二进制数? String num = Integer.toBinaryString(n); int count = 0; char[] c = num.toCharArray(); for (char value :
阅读全文
摘要:public int cuttingRope(int n) { if(n<2) return 0; if(n == 2) return 1; if(n == 3) return 2; int times_of_3 = n/3; if(n%3 == 1){ times_of_3 --; } int t
阅读全文
摘要:class Solution { public int cuttingRope(int n) { int[] dp = new int[n+1]; dp[1] = 1; dp[2] = 1; for(int i = 3;i<=n;i++){ int x1 = i/2; int x2 = i - x1
阅读全文
摘要:public int movingCount(int m, int n, int k) { //暴力法? boolean[][] visited = new boolean[m][n]; return move_count(m,n,k,0,0,visited); } private int move
阅读全文
摘要:public boolean exist(char[][] board, String word) { int m = board.length; int n = board[0].length; boolean[][] visited = new boolean[m][n]; int len =
阅读全文
摘要:class Solution { public int minArray(int[] numbers) { int n = numbers.length; if(n == 1) return numbers[0]; if(n == 2) return Math.min(numbers[0],numb
阅读全文
摘要:动态规划 public int numWays(int n) { if(n == 0 || n == 1) return 1; int[] dp = new int[n+1]; dp[0] = 1; dp[1] = 1; for(int i = 2;i<=n;i++){ int m = dp[i -
阅读全文
摘要:用哈希表 public int fib(int n) { Map<Integer,Integer> map = new HashMap<>(); map.put(0,0); map.put(1,1); if(map.containsKey(n)){ return map.get(n); } for(
阅读全文
摘要:import java.util.Stack; public class CQueue { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); public CQueue() { } public
阅读全文
摘要:递归: public TreeNode buildTree(int[] preorder, int[] inorder) { int len = preorder.length; if(len == 0) return null; TreeNode head = new TreeNode(preor
阅读全文
摘要:虽然很快做出来,但是性能并不好; 用ArrayList做的: public int[] reversePrint(ListNode head) { ArrayList<Integer> list = new ArrayList<>(); while(head != null){ list.add(0
阅读全文
摘要:用了最基本的方法,10分钟才做出来。。 public String replaceSpace(String s) { int n = s.length(); while(true){ int index = s.indexOf(" "); if(index == -1)break; s = s.su
阅读全文
摘要:不难,但是没一下子捋顺,做了46分钟42秒才完成。。 public boolean findNumberIn2DArray(int[][] matrix, int target) { int n = matrix.length; //n行 if(n==0) return false; int m =
阅读全文
摘要:用时4分02秒,用哈希表做的,显然不够优化: public int findRepeatNumber(int[] nums) { Map<Integer,Integer> map = new HashMap<>(); for (int num : nums) { if (!map.containsK
阅读全文
摘要:头依次拔下来,按顺序插进另一个链表: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: d
阅读全文

浙公网安备 33010602011771号