欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Airwallex leetcode 刷题宝典

Airwallex leetcode 刷题宝典

2025/3/18 0:25:41 来源:https://blog.csdn.net/adorechen/article/details/145344435  浏览:    关键词:Airwallex leetcode 刷题宝典

Airwallex是一家很有前景的公司,有想法的小伙伴可以来这里查看该公司的leetcode刷题宝典。

题目1:18. 四数之和 - 力扣(LeetCode)

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abc 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]
class Solution {public List<List<Integer>> fourSum(int[] nums, int target) {// sort the arrayArrays.sort(nums);List<List<Integer>> result = new ArrayList<>();long sum = 0, tar = (long) target, tmpSum;// travel afor (int i=0; i<nums.length-3; i++) {// skip the same tuple if (i != 0 && nums[i-1] == nums[i]) {continue;}// travel bfor (int j=i+1; j<nums.length-2; j++) {// skip the same tupleif (j != i+1 && nums[j-1] == nums[j]) {continue;}sum = tar - nums[i] - nums[j];// get c+d = target-a-b from two directionint left = j+1, right = nums.length-1;while(left < right) {tmpSum = (long)nums[left] + nums[right];if (tmpSum == sum) {result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));left++;// skip duplicate cwhile(left<right && nums[left] == nums[left-1]) left++;right--;} else if (tmpSum > sum) { // move the left side to find a different numberright--;} else { // move the right side to find a different numberleft++;}}}}return result;}
}

题目2:LCR 100. 三角形最小路径和 - 力扣(LeetCode)

给定一个三角形 triangle ,找出自顶向下的最小路径和。

每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。

示例 1:

输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
输出:11
解释:如下面简图所示:23 46 5 7
4 1 8 3
自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。

示例 2:

输入:triangle = [[-10]]
输出:-10
class Solution {public int minimumTotal(List<List<Integer>> triangle) {int[] pathResult = new int[triangle.size()];// calculate path resultfor (int i=0; i<triangle.size(); i++) {List<Integer> row = triangle.get(i);int rowBound = row.size()-1;for (int j=rowBound; j>=0; j--) {if (j==0) { // left bound 0pathResult[j] = pathResult[j] + row.get(j);} else if (j == rowBound) { // right bound pathResult[j] = pathResult[j-1] + row.get(j);} else {pathResult[j] = Math.min(pathResult[j-1], pathResult[j]) + row.get(j);}}}// get minimum path int min = pathResult[0];for (int i=1; i<pathResult.length; i++) {if (pathResult[i] < min) {min = pathResult[i];}}return min;}
}   

 

题目3:283. 移动零 - 力扣(LeetCode)

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

请注意 ,必须在不复制数组的情况下原地对数组进行操作。

示例 1:

输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]

示例 2:

输入: nums = [0]
输出: [0]
class Solution {public void moveZeroes(int[] nums) {int len = nums.length;int[] copyArray = new int[len];for (int i=0, j=0; i<len; i++) {if (nums[i] != 0) {copyArray[j++] = nums[i];}}for (int i=0; i<len; i++) {nums[i] = copyArray[i];}}
}

 

 题目4:78. 子集 - 力扣(LeetCode)

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的

子集

(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同
class Solution {public List<List<Integer>> subsets(int[] nums) {return subsets(nums, nums.length-1);}public List<List<Integer>> subsets(int[] nums, int end) {if (end == 0) {return List.of(List.of(), List.of(nums[0]));}// don't contains last elementList<List<Integer>> notLast = subsets(nums, end-1);List<List<Integer>> result = new ArrayList<>(notLast.size()*2);result.addAll(notLast);// contains last elementfor(List<Integer> list: notLast) {List<Integer> newList = new ArrayList(list.size()+1);newList.addAll(list);newList.add(nums[end]);result.add(newList);}return result;}
}

 

题目5:69. x 的平方根 - 力扣(LeetCode)

给你一个非负整数 x ,计算并返回 x 的 算术平方根 。

由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。

注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。

示例 1:

输入:x = 4
输出:2

示例 2:

输入:x = 8
输出:2
解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。

提示:

  • 0 <= x <= 231 - 1
class Solution {public int mySqrt(int x) {long left = 0, right = x, mid=0, result, target = x;while(left <= right) {mid = (left + right)/2;result = mid * mid;if (result == target) {return (int)mid;} else if (result > target) {right = mid-1;} else {left = mid+1;}}return (int)(left-1);}
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词