add
This commit is contained in:
parent
1241441ffb
commit
bff038b9d5
11
5.leetcode/src/com/fanxb/common/p100/Q136.java
Normal file
11
5.leetcode/src/com/fanxb/common/p100/Q136.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q136 {
|
||||
public int singleNumber(int[] nums) {
|
||||
int res = nums[0];
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
res = res ^ nums[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
19
5.leetcode/src/com/fanxb/common/p100/Q137.java
Normal file
19
5.leetcode/src/com/fanxb/common/p100/Q137.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
public class Q137 {
|
||||
public int singleNumber(int[] nums) {
|
||||
int[] count = new int[32];
|
||||
for (int num : nums) {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
count[i] += num & 1;
|
||||
num = num >>> 1;
|
||||
}
|
||||
}
|
||||
int res = 0;
|
||||
for (int i = 0; i < 32; i++) {
|
||||
res = res | count[i] % 3;
|
||||
res = res << 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
22
5.leetcode/src/com/fanxb/common/p100/Q66.java
Normal file
22
5.leetcode/src/com/fanxb/common/p100/Q66.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.fanxb.common.p100;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q66 {
|
||||
public int[] plusOne(int[] digits) {
|
||||
if (digits[0] == 0) return new int[]{1};
|
||||
int n = digits.length;
|
||||
digits[n - 1]++;
|
||||
boolean hasNext = false;
|
||||
for (int i = digits.length - 1; i >= 0; i--) {
|
||||
digits[i] += hasNext ? 1 : 0;
|
||||
hasNext = digits[i] >= 10;
|
||||
if (hasNext) digits[i] = digits[i] - 10;
|
||||
else return digits;
|
||||
}
|
||||
int[] res = new int[digits.length + 1];
|
||||
res[0] = 1;
|
||||
System.arraycopy(digits, 0, res, 1, res.length - 1);
|
||||
return res;
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ public class Q69 {
|
||||
if (x <= 3) return 1;
|
||||
int l = 2, r = x / 2, m;
|
||||
while ((m = (int) Math.floor((l + r) / 2.0)) != r) {
|
||||
if (m * (long)m > x) {
|
||||
if (m * (long) m > x) {
|
||||
r = m;
|
||||
} else if (m == l) {
|
||||
r--;
|
||||
@ -53,6 +53,32 @@ public class Q69 {
|
||||
return m;
|
||||
}
|
||||
|
||||
public int mySqrt2(int x) {
|
||||
if (x == 0) return 0;
|
||||
long l = 0, r = x;
|
||||
while (l < r) {
|
||||
long mid = (l + r) / 2;
|
||||
long val = mid * mid;
|
||||
if (val == x) return (int) mid;
|
||||
else if (val > x) r = mid;
|
||||
else if (mid == l) return (int) l;
|
||||
else l = mid;
|
||||
}
|
||||
return (int) l;
|
||||
}
|
||||
|
||||
public int mySqrt3(int x) {
|
||||
if (x == 0) return 0;
|
||||
double temp = x;
|
||||
while (true) {
|
||||
double temp1 = (temp + x / temp) / 2;
|
||||
if (Math.abs(temp1 - temp) <= 0.00001) {
|
||||
return (int) temp1;
|
||||
}
|
||||
temp =temp1;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// System.out.println(new Q69().mySqrt(2147395599));
|
||||
System.out.println(new Q69().mySqrt1(2147395599));
|
||||
|
@ -9,23 +9,25 @@ package com.fanxb.common.p100;
|
||||
public class Q9 {
|
||||
|
||||
public boolean isPalindrome(int x) {
|
||||
//小于0的数或者末尾为0的正数肯定不是回文数
|
||||
if (x < 0 || (x > 0 && x % 10 == 0)) {
|
||||
return false;
|
||||
}
|
||||
long temp = 1;
|
||||
while (x / (temp * 10) > 0) {
|
||||
temp = temp * 10;
|
||||
}
|
||||
for (int i = 1; i < temp; temp = temp / 10, i *= 10) {
|
||||
System.out.println(x / i % 10 + ":" + x / temp % 10);
|
||||
if (x / i % 10 != x / temp % 10) {
|
||||
return false;
|
||||
}
|
||||
if (x < 0) return false;
|
||||
String s = String.valueOf(x);
|
||||
int i = 0, j = s.length() - 1;
|
||||
while (i < j) {
|
||||
if (s.charAt(i++) != s.charAt(j--)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isPalindrome1(int x) {
|
||||
if (x < 0) return false;
|
||||
int temp = x, rev = 0;
|
||||
while (temp > 0) {
|
||||
rev = rev * 10 + temp % 10;
|
||||
temp = temp / 10;
|
||||
}
|
||||
return x == rev;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Q9 instance = new Q9();
|
||||
System.out.println(instance.isPalindrome(1212312));
|
||||
|
12
5.leetcode/src/com/fanxb/common/p200/Q172.java
Normal file
12
5.leetcode/src/com/fanxb/common/p200/Q172.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
public class Q172 {
|
||||
public int trailingZeroes(int n) {
|
||||
int res = 0;
|
||||
while (n > 0) {
|
||||
res += n / 5;
|
||||
n = n / 5;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
21
5.leetcode/src/com/fanxb/common/p200/Q191.java
Normal file
21
5.leetcode/src/com/fanxb/common/p200/Q191.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.fanxb.common.p200;
|
||||
|
||||
public class Q191 {
|
||||
public int hammingWeight(int n) {
|
||||
int count = 0;
|
||||
while (n != 0) {
|
||||
count += n & 1;
|
||||
n = n >>> 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public int hammingWeight1(int n) {
|
||||
int count = 0;
|
||||
while (n != 0) {
|
||||
n = n & (n - 1);
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
20
5.leetcode/src/com/fanxb/common/p300/Q201.java
Normal file
20
5.leetcode/src/com/fanxb/common/p300/Q201.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.fanxb.common.p300;
|
||||
|
||||
public class Q201 {
|
||||
public int rangeBitwiseAnd(int left, int right) {
|
||||
int count = 0;
|
||||
while (left < right) {
|
||||
left >>>= 1;
|
||||
right >>>= 1;
|
||||
count++;
|
||||
}
|
||||
return left << count;
|
||||
}
|
||||
|
||||
public int rangeBitwiseAnd1(int left, int right) {
|
||||
while (left < right) {
|
||||
right = right & (right - 1);
|
||||
}
|
||||
return right;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user