Summary


[LeetCode] Remove Duplicates from Sorted Array 解题报告

Given a sorted array, remove the duplicates in place such that each element appear only  once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A =  [1,1,2],
Your function should return length =  2, and A is now  [1,2].
» Solve this problem

[解题思路]
二指针问题。一前一后扫描。

[LeetCode] Remove Duplicates from Sorted Array II 

Follow up for "Remove Duplicates":
What if duplicates are allowed at most  twice?
For example,
Given sorted array A =  [1,1,1,2,2,3],
Your function should return length =  5, and A is now  [1,1,2,2,3].
» Solve this problem


[解题思路]
加一个变量track一下字符出现次数即可,这题因为是已经排序的数组,所以一个变量即可解决。但是如果是没有排序的数组,可以引入一个hashmap来处理出现次数。

[LeetCode] Remove Duplicates from Sorted List 

Given a sorted linked list, delete all duplicates such that each element appear only  once.
For example,
Given  1->1->2, return  1->2.
Given  1->1->2->3->3, return  1->2->3.
» Solve this problem

[解题思路]
同样是双指针,但是这里要注意delete不用的节点。


[LeetCode] Remove Element 解题报告

Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
» Solve this problem

[解题思路]
双指针。

[LeetCode] Remove Nth Node From End of List 

Given a linked list, remove the  n th node from the end of list and return its head.
For example,
   Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given  n will always be valid.
Try to do this in one pass.
» Solve this problem

[解题思路]
经典题。双指针,一个指针先走n步,然后两个同步走,直到第一个走到终点,第二个指针就是需要删除的节点。唯一要注意的就是头节点的处理,比如,
1->2->NULL, n =2; 这时,要删除的就是头节点。

[LeetCode] Restore IP Addresses 解题报告

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given  "25525511135",
return  ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
» Solve this problem

[解题思路]
递归的将数字串分成四个部分,每个部分满足0<=p<=255。要注意一些边界case,比如010是没有意思的,“0.10. 010.1”。

[LeetCode] Reverse Integer 解题报告

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

[解题思路]
如果是用字符串来逆序的话,需要考虑0的特殊性,但是如果直接用一个integer来滚动生成的话,0就不是问题了。
当然,溢出的问题是存在的,所以return -1。



[LeetCode] Reverse Linked List II 解题报告

Reverse a linked list from position  m to  n. Do it in-place and in one-pass.
For example:
Given  1->2->3->4->5->NULLm = 2 and  n = 4,
return  1->4->3->2->5->NULL.
Note:
Given  mn satisfy the following condition:
1 ≤  m ≤  n ≤ length of list.

[解题思路]
分三步走
1. 找到m节点的前一个指针pre(加个safe guard可避免头指针的问题)
2. 从m节点开始往后reverse N个节点(双指针,cur,post)
3. 合并pre链表,cur链表及post链表。

这题难就难在繁琐上,要考虑各种边界条件,比如
{1,2,3}, 3,3
{1,2,3}, 1,1
{1,2,3}, 1,3
所以,code中需要添加一些边界检查条件。这题15分钟以内bug free我是做不到。


[LeetCode] Roman To Integer 解题报告


Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
» Solve this problem

[解题思路]

从前往后扫描,用一个临时变量记录分段数字。
  • 如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1
  • 否则,将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1

[LeetCode] Rotate Image 解题报告

You are given an  n x  n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
» Solve this problem

[解题思路]
如下图,首先沿逆对角线翻转一次,然后按x轴中线翻转一次。
Picture5.png


[LeetCode] Rotate List 解题报告

Given a list, rotate the list to the right by  k places, where  k is non-negative.
For example:
Given  1->2->3->4->5->NULL and  k =  2,
return  4->5->1->2->3->NULL.
» Solve this problem

[解题思路]
首先从head开始跑,直到最后一个节点,这时可以得出链表长度len。然后将尾指针指向头指针, 将整个圈连起来,接着往前跑len – k%len,从这里断开,就是要求的结果了。

转载于:https://www.cnblogs.com/codingtmd/archive/2013/02/21/5078902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值