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

立贴!从今天开始刷题。

🔗
 楼主| ttgao 2019-5-4 12:01:36 | 只看该作者
全局:
189. Rotate Array

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-4 12:46:45 | 只看该作者
全局:
先把K按数组的长度取一个余,因为如果K是长度的N倍,中间很多次是浪费白做的。

然后用长度减去取出的余,这个就是第一个数字。然后一次填如数组。当K超过数组长度以后,K设置成0,然后从头开始放。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 04:06:05 | 只看该作者
全局:
746. Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

Note:

    cost will have a length in the range [2, 1000].
    Every cost[i] will be an integer in the range [0, 999].
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 04:07:45 | 只看该作者
全局:
此题好像应该用DP来做,不过我暂时只会用递归的办法。

就是求N阶的价格换成N阶价格等于N-1和N-2阶里面价格低的那个。

最后N等于0和1的时候直接等于0.
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 10:58:54 | 只看该作者
全局:
15. 3Sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 11:01:22 | 只看该作者
全局:
此题应该用双指针的办法,先将整个数组排序,然后取出第一个值,然后对剩余的数组使用双指针访问。大于零,就是右指针减少1,小于零就是左指针加上1.

等于零就是左右指针都加一减一。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 12:47:44 | 只看该作者
全局:
48. Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

Example 2:

Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
],

rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-5 12:49:09 | 只看该作者
全局:
这题挺有意思,看了解答才做出的题目。简单讲先是沿着斜线对折数据。然后再沿着竖线再对折。
回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 02:12:45 | 只看该作者
全局:
78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

回复

使用道具 举报

🔗
 楼主| ttgao 2019-5-6 02:15:09 | 只看该作者
全局:
本帖最后由 ttgao 于 2019-5-6 02:27 编辑

此题需要用到递归,C#的解法有一个陷阱。

就是当加入LIST<int>的时候,加入的其实是引用,这样意味着,如果List<int>改变,整个值也该表。


回复

使用道具 举报

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

本版积分规则

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