面试笔试杂项积累-leetcode 1-5

随机刷一刷,有些题有点想的焦头烂额啊,哈哈,不过刷题真上瘾

1.47-全排列2 Permutations II-Difficulty: Medium

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

public class Solution {
    public IList<IList<int>> PermuteUnique(int[] nums) {
                IList<IList<int>> ret = new List<IList<int>>();
        List<int> num = new List<int>();

        for (int i = 0; i < nums.Length; i++)
            num.Add(nums[i]);

            if (num.Count == 0)
                return ret;
        if (num.Count == 1)
        {
            ret.Add(num);
            return ret;
        }

        num.Sort();

        ret.Add(new List<int>(num));
        while (NextPermutationII(num))
            ret.Add(new List<int>(num));

        return ret;
    }
    
    public static bool NextPermutationII(List<int> num)
    {
        if (num.Count <= 1)
            return false;

        //find the falling edge
        int edge = -1;
        for (int i = num.Count - 2; i >= 0; i--)
        {
            if (num[i] < num[i + 1])
            {
                edge = i;
                break;
            }
        }

        if (edge > -1)
        {
            //find replacement
            for (int i = edge + 1; i < num.Count; i++)
            {
                if (num[edge] >= num[i])
                {
                    NextPermutationSwap(num, edge, i - 1);
                    break;
                }
                if (i == num.Count - 1)
                {
                    NextPermutationSwap(num, edge, i);
                    break;
                }
            }
        }
        else
        {
            return false;
        }

        for (int i = edge + 1, j = num.Count - 1; i <= edge + (num.Count - edge - 1) / 2; i++, j--)
        {
            NextPermutationSwap(num, i, j);
        }
        return true;

    }
    public static void NextPermutationSwap(List<int> num, int i, int j)
    {
        int temp = num[i];
        num[i] = num[j];
        num[j] = temp;
    }
}

2.100-Same Tree-Difficulty: Easy

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.



/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public bool IsSameTree(TreeNode p, TreeNode q) {
        if(p!=null&&q!=null)
    {  
        if(p.val == q.val)
        {
             if(IsSameTree(p.left,q.left))
            {  
                 if(IsSameTree(p.right,q.right))
                 return true;
                  else
                  return false;
              }
         else
             return false;
            
        }
        else
        {
            return false;
            
        }
    } 
    else if((p!=null&&q == null)||(q!=null&&p == null))
        {
            return false;
        }
else
return true;
}
}

3.6-ZigZag Conversion-Difficulty: Easy

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".


分析

Zigzag:即循环对角线结构(

0   8   16   
1  79  1517   
2 6 10 14 18   
35  1113  19   
4   12   20   

public class Solution {
    public string Convert(string s, int numRows) {
        
     if (numRows <= 1 || numRows >= s.Length)//numRows>s.Length防止列数大于所排个数
            return s;



        string str = "";
        int length = s.Length;
        int temp = 0;

        for (int i = 0; i < numRows; i++)
        {
            //     str += s[numRows - 1];

            for (int j = 0; j < length / ((numRows - 2) + numRows) + 1; j++)
            {
                temp = (i) + j * ((numRows - 2) + numRows) ;
                if (temp >= s.Length || temp < 0)
                    break;
                else
                    str += s[temp];//竖
                if (i != 0 && i != numRows - 1)
                {
                    temp = -(i - 1) + (j + 1) * ((numRows - 2) + numRows) - 1;
                    if (temp >= s.Length || temp < 0)
                        break;
                    else
                        str += s[temp];//斜弯折

                }
            }
        }


        return str;
    }
}

4.1-Two Sum-Difficulty: Medium

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
       if (nums.Length < 2)
        {
            return null;
        }

        int[] index = new int[2];

        for (int i = 0; i < nums.Length; i++)
        {
        //    if (nums[i] <= target)//只有正数
            index[0] = i + 1;//正负皆可
        //    else
        //        continue;

            for (int j = 0; j < nums.Length; j++)
            {
                if (j != i)
                {
                    if (nums[j] + nums[index[0] - 1] == target)
                    {
                        index[1] = j + 1;
                        return index;

                    }

                }
            }

        }


        return null;
    }
}

5.2-Add Two Numbers-Difficulty: Medium


You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8


分析

遍历l1,l2,把l2加到l1上,返回l1链表,逢十进一,如果刚好在l1末,则new一个节点值为1,若l1先为空(l1比l2短)则遍历l2后半段赋予l1新节点上,若l2先为空(l2比l1短)则判断进一后直接返回

/**
 * 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 AddTwoNumbers(ListNode l1, ListNode l2) {
           ListNode l3 = l1;
        while (l2 != null)
        {
            if (l1 != null && l2 != null)
            {
                l1.val = l1.val + l2.val;
                if (l1.val >= 10)
                {
                    l1.val -= 10;
                    if (l1.next != null)
                    {
                        l1.next.val += 1;
                        if (l2.next == null)
                        {
                            while (l1.next != null)
                            {
                                if (l1.next.val >= 10)
                                {
                                    l1.next.val -= 10;
                                    if (l1.next.next == null)
                                    {
                                        l1.next.next = new ListNode(1);
                                        return l3;
                                    }
                                    else
                                    {
                                        l1 = l1.next;
                                        l1.next.val += 1;
                                    }
                                }
                                else
                                    return l3;
                            }
                            return l3;
                        }
                    }
                    else
                    {
                        l1.next = new ListNode(1);
                    }
                }
                l2 = l2.next;
                if (l1.next == null)
                {
                    while (l2 != null)
                    {
                        if (l1.next == null)
                        {
                            l1.next = new ListNode(l2.val);
                            if (l1.next.val >= 10)
                            {
                                l1.next.val -= 10;
                                l1.next.next = new ListNode(1);
                            }
                            l1 = l1.next;
                            l2 = l2.next;
                        }
                    }
                    return l3;
                }
                l1 = l1.next;
            }
            else if (l1 == null && l2 == null)
            {
                break;
            }
        }
        return l3;
    }
}













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值