Java设计实践课的LeetCode题目

7.Reverse Integer 

解题思路:使用强制类型转换的代码,先用long做,之后判断值然后强制转化成int

 1 public class Solution {
 2     public int reverse(int x) {
 3         long reverse_n = 0;
 4         while (x != 0) {
 5             reverse_n = reverse_n * 10 + x % 10;//关键代码
 6             x = x / 10;
 7         }
 8         if (reverse_n > Integer.MAX_VALUE || reverse_n < Integer.MIN_VALUE) {
 9             return 0;
10         }
11         return (int)reverse_n;
12     }
13 }

 9. Palindrome Number 

 1 public class Solution {//解题思路:直接利用直接做的reverse题目,如果x == reverse_x则说明是回文数。
 2     public boolean isPalindrome(int x) {
 3         if (x < 0) {
 4             return false;
 5         }
 6         int y = x;
 7         long reverse_x = 0;
 8         while (x != 0) {
 9             reverse_x = reverse_x * 10 + x % 10;
10             x = x / 10;
11         }
12         // if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
13         //     return false;
14         // }
15         if (y == (int)reverse_x) {
16             return true;
17         }
18         return false;
19     }
20 }

 8. String to Integer (atoi)

 1 public class Solution {
 2     public int myAtoi(String str) {
 3         //输入有效性判断
 4         if (str == null || str.length() == 0) {
 5             return 0;
 6         }
 7         int index = 0;
 8         int sign = 1;
 9         int sum = 0;
10         //找到第一个不是空格的字符
11         while (str.charAt(index) == ' ' && index < str.length()) {
12             index++;
13         }
14         //判断正负
15         if (str.charAt(index) == '+' || str.charAt(index) == '-') {
16             sign = str.charAt(index) == '+' ? 1 : -1;
17             index++;
18         }
19         //防止溢出
20         while (index < str.length() && str.charAt(index) >= '0' && str.charAt(index) <= '9') {
21             int num = str.charAt(index) - '0';
22             if (sum > Integer.MAX_VALUE / 10 || (sum == Integer.MAX_VALUE / 10 && num > Integer.MAX_VALUE % 10)) {
23                 return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
24             }
25             sum = sum * 10 + num;
26             index++;
27         }
28         return sum * sign;
29     }
30 }
View Code

581. Shortest Unsorted Continuous Subarray (需要一个额外的数组对现有数组进行排序,然后找到第一个跟排序数组不同的数字的索引)                

 1 public class Solution {
 2     public int findUnsortedSubarray(int[] nums) {
 3         if (nums == null || nums.length == 0) {
 4             return 0;
 5         }
 6         //将数组复制出去并进行排序
 7         int numsLen = nums.length;
 8         int[] temp = new int[numsLen];
 9         for (int i = 0; i < numsLen; i++) {
10             temp[i] = nums[i]; 
11         }
12         Arrays.sort(temp);
13         //分别从左到右找到第一个跟排序数组不一样的数字
14         int left = 0;
15         int right = nums.length - 1;
16         while (left < numsLen - 1 && nums[left] == temp[left]) {
17             left++;
18         }
19         while (right > 0 && nums[right] == temp[right]) {
20             right--;
21         }
22         //排除已经排序好的情况
23         if (right <= left) {
24             return 0;
25         } else {
26             return right - left + 1;
27         }
28     }
29 }
View Code

 456. 132 Pattern (time:O(n*n))                  

 1 public class Solution {
 2     public boolean find132pattern(int[] nums) {
 3         if (nums == null || nums.length < 3) {
 4             return false;
 5         }
 6         for (int i = 1, min = nums[0]; i < nums.length; i++) {
 7             if (nums[i] < min) {
 8                 min = nums[i];
 9                 continue;
10             }
11             for (int j = nums.length - 1; j > i; j--) {
12                 if (min < nums[j] && nums[j] < nums[i]) {
13                     return true;
14                 }
15             }
16         }
17         return false;
18     }
19 }
View Code

 476. Number Complement

highestOneBit~~                   

1 public class Solution {
2     public int findComplement(int num) {
3         //hightestOneBit为获取最高位为1,其余为0,返回int
4         return ~num & (Integer.highestOneBit(num) - 1);
5     }
6 }
View Code
 1 public class Solution {
 2     public int findComplement(int num) {
 3        //用全为1减去num
 4        int sum = 0;
 5        int i = 0;
 6        while (sum < num) {
 7            sum += Math.pow(2,i);
 8            i++;
 9        }
10        return sum - num;
11     }
12 }
View Code

 306. Additive Number                   

 1 public class Solution {
 2     public boolean isAdditiveNumber(String num) {
 3         if (num.length() <= 2) {
 4             return false;
 5         }
 6         int len = num.length();
 7         //选择第一个数
 8         for (int i = 1; i <= (len - 1) / 2; i++) {
 9             //第一个数首数字不能为0
10             if (num.charAt(0) == '0' && i >= 2) {
11                 break;
12             }
13             //选择第二个数
14             for (int j = i + 1; len - j >= i && len - j >= j - i; j++) {
15                 //第二个谁首数字不能为0
16                 if (num.charAt(i) == '0' && j - i >= 2) {
17                     break;
18                 }
19                 //截取前两个数字
20                 String num1 = num.substring(0, i);
21                 String num2 = num.substring(i, j);
22                 String sum = num.substring(j);
23                 if (isAdditive(num1, num2, sum)) {
24                     return true;
25                 }
26             }
27         }
28         return false;
29     }
30     public boolean isAdditive(String num1, String num2, String sum) {
31         //如果已经运行到最后一个数字为空表示之前的isAdditive是满足的
32         if (sum.equals("")) {
33             return true;
34         }
35         long x1 = Long.parseLong(num1);
36         long x2 = Long.parseLong(num2);
37         String result = ((Long)(x1 + x2)).toString();
38         if (!sum.startsWith(result)) {
39             return false;
40         }
41         return isAdditive(num2, result, sum.substring(result.length()));
42     }
43 }
View Code

543. Diameter of Binary Tree                   

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int diameterOfBinaryTree(TreeNode root) {
12         if (root == null) {
13             return 0;
14         }
15         int depthSum = depth(root.left) + depth(root.right);
16         //最大直径:如果包含根节点,那就是左边的深度+右边的深度,如果不包含根节点,分别以root.left和root.right为新的根节点重复操作
17         return Math.max(depthSum, Math.max(diameterOfBinaryTree(root.left), diameterOfBinaryTree(root.right)));
18     }
19     public int depth(TreeNode root) {
20         //求数的深度
21         if (root == null) {
22             return 0;
23         }
24         return 1 + Math.max(depth(root.left), depth(root.right));
25     }
26 }
View Code

279. Perfect Squares 

   

 

      

 1 public class Solution {
 2     public int numSquares(int n) {
 3         if (n <= 0) {
 4             return 0;
 5         }
 6         //正着计算出所有的dp[n]
 7         int[] dp = new int[n + 1];
 8         for (int i = 1; i <= n; i++) {
 9             int min = Integer.MAX_VALUE;
10             for (int j = 1; j * j <= i; j++) {
11                 min = Math.min(min, dp[i - j * j] + 1);
12             }
13             dp[i] = min;
14         }
15         return dp[n];
16     }
17 }
View Code

 

 

         

posted @ 2017-04-25 21:29  木子月月鸟  阅读(211)  评论(0编辑  收藏  举报