📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: ttgao
跳转到指定楼层
上一主题 下一主题
收起左侧

立贴!从今天开始刷题。

🔗
 楼主| ttgao 2019-5-6 02:15:40 | 只看该作者
全局:
顺便吐槽一下,本论坛居然没有C#的代码支持。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 02:27:37 | 只看该作者
全局:
  1. public class Solution {
  2.     public IList<IList<int>> Subsets(int[] nums) {
  3.         IList<IList<int>> result = new List<IList<int>>();
  4.         IList<int> subset = new List<int>();
  5.         Helper(result,subset,nums,0);
  6.         return result;
  7.     }
  8.    
  9.     public void Helper(IList<IList<int>> res,IList<int> subset,int[] nums,int index)
  10.     {
  11.         res.Add(new List<int>(subset));

  12.         for (int i = index; i < nums.Length; i++)
  13.         {
  14.             subset.Add(nums);
  15.             Helper(res, subset, nums, i + 1);
  16.             subset.RemoveAt(subset.Count - 1);
  17.         }
  18.     }
  19. }
复制代码
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 06:18:13 | 只看该作者
全局:
238. Product of Array Except Self

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 06:20:12 | 只看该作者
全局:
这题比较傻瓜,感觉不会考。简单讲就是把所有左边的乘积取出来,然后乘上右边的乘积。

算左边的时候一个loop,算右边的时候一个loop。这样就可以用2个O(n)。2个O(n)还是O(n)。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 10:07:13 | 只看该作者
全局:
Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

Example 1:

Input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input:
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output:
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up:

    A straight forward solution using O(mn) space is probably a bad idea.
    A simple improvement uses O(m + n) space, but still not the best solution.
    Could you devise a constant space solution?
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 10:07:57 | 只看该作者
全局:
此题难度一般,但是follow up感觉非常难。我是没啥好办法,不知道有大神能给指点一下嘛?
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 12:00:51 | 只看该作者
全局:
380. Insert Delete GetRandom O(1)


Design a data structure that supports all following operations in average O(1) time.

    insert(val): Inserts an item val to the set if not already present.
    remove(val): Removes an item val from the set if present.
    getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

Example:

// Init an empty set.
RandomizedSet randomSet = new RandomizedSet();

// Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomSet.insert(1);

// Returns false as 2 does not exist in the set.
randomSet.remove(2);

// Inserts 2 to the set, returns true. Set now contains [1,2].
randomSet.insert(2);

// getRandom should return either 1 or 2 randomly.
randomSet.getRandom();

// Removes 1 from the set, returns true. Set now contains [2].
randomSet.remove(1);

// 2 was already in the set, so return false.
randomSet.insert(2);

// Since 2 is the only number in the set, getRandom always return 2.
randomSet.getRandom();
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 12:02:25 | 只看该作者
全局:
此题有点难度,就是用hast表来插入和删除,用数组来随机返回数字。

删除的时候,要在数组里面相应删除对应的数据,所以必须先和数组最后一个元素交换然后再删删除。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-9 13:53:39 | 只看该作者
全局:
21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-9 13:54:09 | 只看该作者
全局:
好长时间不使用链表了,这个链表要好好熟悉一下。
回复

使用道具 举报

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

本版积分规则

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