楼主: zado
跳转到指定楼层
上一主题 下一主题
收起左侧

Google Onsite面经

🔗
bobzhang2004 2015-12-6 00:56:03 | 只看该作者
全局:
请问k sum smaller使用递归吗?private static void helper(int[] arr, int target, int start, int k) ?
回复

使用道具 举报

🔗
bobzhang2004 2015-12-6 01:12:12 | 只看该作者
全局:
写了下k sum,欢迎指教
  1. public class KSumSmaller {

  2.         public static void main(String[] args) {
  3.                 int[] arr = {3, 1, 5, 2, 4};
  4.                 int res = findKSumSmaller(arr, 9, 3);
  5.                 System.out.println(res);
  6.         }
  7.        
  8.         static int count = 0;
  9.         public static int findKSumSmaller(int[] arr, int target, int k) {
  10.                 if (arr == null || arr.length == 0) {
  11.                         return 0;
  12.                 }
  13.                 Arrays.sort(arr);
  14.                 if (k == 1) {
  15.                         for (int i : arr) {
  16.                                 if (i < target) {
  17.                                         count += 1;
  18.                                 }
  19.                         }
  20.                 }  else if (k == 2) {
  21.                         findTwoSumSmaller(arr, target, 0);
  22.                 } else {
  23.                         helper(arr, target, 0, k);
  24.                 }
  25.                 return count;
  26.         }
  27.        
  28.         public static void findTwoSumSmaller(int[] arr, int target, int start) {
  29.                 if (arr == null || arr.length == 0) {
  30.                         return;
  31.                 }
  32.                 Arrays.sort(arr);
  33.                 int left = start;
  34.                 int right = arr.length - 1;
  35.                 while (left < right) {
  36.                         int sum = arr[left] + arr[right];
  37.                         if (sum < target) {
  38.                                 count += right - left;
  39.                                 left++;
  40.                         } else {
  41.                                 right--;
  42.                         }
  43.                 }
  44.         }

  45.         private static void helper(int[] arr, int target, int start, int k) {
  46.                 if (start >= arr.length) {
  47.                         return;
  48.                 }
  49.                 if (k == 2) {
  50.                         findTwoSumSmaller(arr, target, start);
  51.                 } else {
  52.                         if (arr[start] < target) {
  53.                                 helper(arr, target - arr[start], start + 1, k - 1);
  54.                                 helper(arr, target, start + 1, k);
  55.                         }
  56.                 }
  57.         }
  58. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表