0510-II173942555757-II64
Posted 保护眼睛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0510-II173942555757-II64相关的知识,希望对你有一定的参考价值。
剑指offer
剑指 Offer 42. 连续子数组的最大和
class Solution42 {
public int maxSubArray(int[] nums) {
int len = nums.length;
int tmpRes = 0;
int res = nums[0];
for (int i = 0; i < len; i++) {
tmpRes = Math.max(nums[i], tmpRes + nums[i]);
res = Math.max(tmpRes, res);
}
return res;
}
}
剑指 Offer 10- II. 青蛙跳台阶问题
class Solution10 {
public static int numWays(int n) {
if (n <= 2)
return n;
int f1 = 1;
int f2 = 2;
int res = 0;
for (int i = 3; i <= n; i++) {
res = (f1 + f2) % 1000000007;
f1 = f2;
f2 = res;
}
return res;
}
}
剑指 Offer 05. 替换空格
class Solution05 {
public static String replaceSpace(String s) {
return s.replace(" ", "%20");
}
}
剑指 Offer 55 - I. 二叉树的深度
class Solution55 {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
public int maxDepthDfs(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;
while (!queue.isEmpty()) {
res++;
int size = queue.size();
while (size-- > 0) {
TreeNode cur = queue.poll();
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
}
return res;
}
}
剑指 Offer 17. 打印从1到最大的n位数
class Solution17 {
public static int[] printNumbers(int n) {
StringBuilder len = new StringBuilder("1");
for (int i = 0; i < n; i++) {
len.append(0);
}
int[] res = new int[Integer.parseInt(len.toString()) - 1];
for (int i = 1; i <= Integer.parseInt(len.toString()) - 1; i++) {
res[i - 1] = i;
}
return res;
}
}
剑指 Offer 57 - II. 和为s的连续正数序列
class Solution57_II {
public int[][] findContinuousSequence(int target) {
List<int[]> res = new ArrayList<>();
int left = 1;
int right = 1;
int sum = 0;
while (left <= target / 2) {
if (sum < target) {
sum += right;
right++;
} else if (sum > target) {
sum -= left;
left++;
} else {
int[] tmp = new int[right - left];
for (int i = left; i < right; i++) {
tmp[i - left] = i;
}
res.add(tmp);
sum -= left;
left++;
}
}
return res.toArray(new int[res.size()][]);
}
}
剑指 Offer 57. 和为s的两个数字
class Solution57 {
public int[] twoSum(int[] nums, int target) {
int i = 0, j = nums.length - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
return new int[]{nums[i], nums[j]};
} else if (sum > target) {
j--;
} else {
i++;
}
}
return new int[0];
}
}
剑指 Offer 64. 求1+2+…+n
class Solution64 {
public static int sumNums(int n) {
boolean flag = n > 0 && (n += sumNums(n - 1)) == 0;
return n;
}
public static int sumNums2(int n) {
boolean flag = n <= 0 || (n += sumNums2(n - 1)) > 0;
return n;
}
public static void main(String[] args) {
System.out.println(sumNums2(5));
}
}
剑指 Offer 39. 数组中出现次数超过一半的数字
class Solution39 {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}
}
class Solution39_2 {
private Map<Integer, Integer> countNums(int[] nums) {
Map<Integer, Integer> counts = new HashMap<>();
for (int num : nums) {
if (!counts.containsKey(num)) {
counts.put(num, 1);
} else {
counts.put(num, counts.get(num) + 1);
}
}
return counts;
}
public int majorityElement(int[] nums) {
Map<Integer, Integer> counts = countNums(nums);
int len = nums.length;
for (Map.Entry<Integer,Integer> entry : counts.entrySet()) {
if (entry.getValue() > len / 2){
return entry.getKey();
}
}
return -1;
}
}
以上是关于0510-II173942555757-II64的主要内容,如果未能解决你的问题,请参考以下文章
sqli-labs less64 GET -Challenge -Blind -130 queries allowed -Variation3 (GET型 挑战 盲注 只允许130次查询 变化3)(代
Android 安装包优化动态库打包配置 ( “armeabi-v7a“, “arm64-v8a“, “x86“, “x86_64“ APK 打包 CPU 指令集配置 | NDK 完整配置参考 )(代
记录mysql 5.7.20安装 出现...mysql-5.7.20-winx64datais_writable’ Errcode: 2 - No such file or directory(示(代