面试笔试杂项积累-leetcode 86-90

86.86-Partition List-Difficulty: Medium

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

思路

把小于target的都堆到前面去

先找到第一个大于或等于target的节点,然后往它前面堆就好

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode Partition(ListNode head, int x) {
        if(head == null||head.next == null)
        return head;
             ListNode temp = new ListNode(0);
        temp.next = head;
        ListNode fin = temp;
        ListNode pointer = temp;//记录前面的小值的位置
        ListNode tran;
          while (temp.next != null)
        {
            if (temp.next.val < x)
            {
                temp = temp.next;
                pointer = pointer.next;
            }
            else
                break;
        }
        while (temp.next != null)
        {

            if (temp.next.val < x)
            {

                tran = pointer.next;
                pointer.next = temp.next;
                temp.next = temp.next.next;
                pointer.next.next = tran;
                pointer = pointer.next;
            }
          else
          {
            temp = temp.next;
          }
         if(temp == null)
            break;

        }
        return fin.next;
    }
}





88.88-Merge Sorted Array-Difficulty: Easy

Given two sorted integer arrays nums1 andnums2, merge nums2 into nums1 as one sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal tom + n) to hold additional elements from nums2. The number of elements initialized innums1 and nums2 are m and n respectively.

思路

合并两个排好序的数组。

使用两个pointers作为位置标记,遍历判断赋值

另外注意

<pre name="code" class="csharp"><span style="font-size:14px;">if (pointer1 < m && ((pointer2 >= n) || _nums1[pointer1] <= nums2[pointer2]))</span>
pointer1 < m && ((pointer2 >= n)

 
这样的pointer与长度判断放在前面,以免使判断超过长度 

public class Solution {
    public void Merge(int[] nums1, int m, int[] nums2, int n) {
        if(n == 0)
        return;
                int pointer1 = 0;
        int pointer2 = 0;
        int[] _nums1 = new int[m];
        Array.Copy(nums1, _nums1, m);
        for (int i = 0; i < nums1.Length; i++)
        {
            if (pointer1 < m && ((pointer2 >= n) || _nums1[pointer1] <= nums2[pointer2]))
            {
                nums1[i] = _nums1[pointer1];
                ++pointer1;
            }
            else if (pointer2 < n && (pointer1 >= m || nums2[pointer2] <= _nums1[pointer1]))
            {
                nums1[i] = nums2[pointer2];
                ++pointer2;
            }
            else
                break;

        }
    }
}


89.89-Gray Code-Difficulty: Medium

The gray code is a binary numeral system where two successive values differ in only one bit.

Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note:
For a given n, a gray code sequence is not uniquely defined.

For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

思路

格雷码,返回所有n位和小于n位的所有二进制对应的十进制,不能重复,顺序固定,

动态规划

并且使用一个很机智的运算符<<左移。。。

举例:

一,问:计算表达式14 << 2的值。
答:表达式14 << 2的值为56,因为14(即二进制的00001110)向左移两位等于56(即二进制的00111000)。
二,问: 计算表达式8 >> 2的值。
答:表达式8 >> 2的值为2,因为8(即二进制的00001000)向右移两位等于2(即二进制的00000010)。

参考:

http://blog.csdn.net/u013897903/article/details/44799237

public class Solution {
  public IList<int> GrayCode(int n)
    {
        IList<int> result = new List<int>();
        if (n < 0) return result;
        if (n == 0)
        {
            result.Add(0);
            return result;
        }
           IList<int> last = GrayCode(n - 1);
        int addNum = 1 << (n - 1);
        for (int i = last.Count - 1; i >= 0; i--)
        {
            last.Add(last[i] + addNum);
        }
        return last;  
    }
}

90.90-Subsets II-Difficulty: Medium

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

思路

与Subsets 78题唯一不同的地方是,这个有重复值,所以产生的结果可能重复

我们能做的就是在78题回溯的基础上加上去重复步骤

public class Solution {
     IList<IList<int>> fin = new List<IList<int>>();
    int _n = 0;
    int[] _nums;
  public IList<IList<int>> SubsetsWithDup(int[] nums) {
        Array.Sort(nums);//要求输出从小到大的顺序
        _nums = nums;

        IList<int> temp = new List<int>();
        fin.Add(temp);

        _n = nums.Length;

        recursion(0, temp);//传start+1不是i+1,就不是从小到大了,还有重复的数

        return fin;
    }
    void recursion(int start, IList<int> temp)
    {
          for (int i = start; i < _n/* _n - _k + 1*/; i++)
        {

             temp.Add(_nums[i]);//每temp add完就add入fin
            fin.Add(new List<int>(temp));
            recursion(i + 1, temp);//传start+1不是i+1,就不是从小到大了,还有重复的数

            if (temp.Count > 0)
                temp.RemoveAt(temp.Count - 1);
                
                while (i < _n-1&&_nums[i] == _nums[i + 1])//去掉重复部分即可//这块很重要,一次递归之后立刻把重复值全部不要,++i
        {   
            ++i;

            }
        }
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值