- 博客(13)
- 收藏
- 关注
原创 剑指 Offer 55 - I. 二叉树的深度
/**Definition for a binary tree node.public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }}*/class Solution {public int maxDepth(TreeNode root) {if (root==null) {return 0;}return Math.max(maxDep
2021-04-27 17:43:49 58
原创 剑指 Offer 58 - II. 左旋转字符串
class Solution {public String reverseLeftWords(String s, int n) {return s.substring(n)+s.substring(0,n);}}class Solution {public String reverseLeftWords(String s, int n) {String result = “”;for(int i=n; i<s.length(); i++) {result += s.charAt(i)
2021-04-27 17:35:31 74
原创 整数的二进制形式中1的个数
import java.util.Scanner;public class Solution {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();String s = Integer.toBinaryString(n);System.out.println(s);char[] chars = s.toCharArray();int count = 0
2021-04-25 00:03:19 66
原创 191. 位1的个数
位运算public class Solution {// you need to treat n as an unsigned valuepublic int hammingWeight(int n) {int count = 0;for (int i=0; i<32; i++) {if ((n & (1<<i))!=0 ) {count ++;}}return count;}}
2021-04-24 23:41:50 54
原创 指 Offer 22. 链表中倒数第k个节点
/**Definition for singly-linked list.public class ListNode {int val;ListNode next;ListNode(int x) { val = x; }}*/class Solution {public ListNode getKthFromEnd(ListNode head, int k) {ListNode fast = head;for (int i=1; i<k; i++) {fas
2021-04-24 22:00:27 49
原创 1154.一年中的第几天
import java.util.Scanner;public class Solution {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.nextLine();String s1 = s.substring(4,6);String s2 = s.substring(6);int m = Integer.parseInt(s1);int n = Intege
2021-04-24 21:28:32 63
原创 88.合并两个有序数组
class Solution {public void merge(int[] nums1, int m, int[] nums2, int n) {for (int i=0; i<n; i++) {nums1[m+i] = nums2[i];}Arrays.sort(nums1);}}
2021-04-24 20:56:52 52
原创 剑指offer50第一个只出现一次的字符
class Solution {public char firstUniqChar(String s) {char[] chars = s.toCharArray();Map<Character, Boolean> map = new HashMap<>();for (int i=0; i<chars.length; i++) {map.put(chars[i],map.containsKey(chars[i]));}for (int i=0; i<char
2021-04-24 17:31:31 53
原创 14.最长公共前缀
class Solution {public String longestCommonPrefix(String[] strs) {if (strs.length0) {return “”;}if (strs.length1) {return strs[0];}String result = strs[0];for (int i=0; i<strs.length; i++) {while (!strs[i].startsWith(result)) {result = result
2021-04-24 15:39:22 68
原创 斐波那契数列
class Solution {public int fib(int n) {if (n<=1) {return n;}int[] dp = new int[n+1];dp[0] = 0;dp[1] = 1;for(int i=2; i<=n; i++) {dp[i] = dp[i-1]+dp[i-2];}return dp[n];}}
2021-04-24 02:21:57 54
原创 抓石子游戏
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int m = sc.nextInt();int n = sc.nextInt();if (m%(n+1)==0) {System.out.println(“false”);}else System.out.println(“true”);}}.
2021-04-24 02:17:19 177
原创 找到数组中重复出现的数字
class Solution {public int findRepeatNumber(int[] nums) {Set set = new HashSet();for(int i=0; i<nums.length; i++) {if(!set.add(nums[i])) {return nums[i];}}return -1;}}
2021-04-24 02:01:37 177
原创 字符串中数字、英文字符、中文字符的统计
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.nextLine();char[] chars = s.toCharArray();int count1 = 0;//数字int count2 = 0;//英文字符int count3 = 0;//中文字符for (int i
2021-04-24 01:42:18 265
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人