面试笔试杂项积累-leetcode 151-155

来源:互联网 发布:海康提示网络不可达 编辑:程序博客网 时间:2024/05/20 23:03

151.151-Linked List Cycle-Difficulty: Medium

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

click to show clarification.

Clarification:
  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

思路

单词反过来

博主的方法是从后往前遍历一遍,遇到空格即是单词结束,Substring取这个单词,加到最终结果的temp中


public class Solution {    public string ReverseWords(string s) {           string fin = "";        for (int i = s.Length - 1; i > -1; i--)        {            int temp = 1;            if (s[i] != 32)            {                while (i - temp >= 0 && s[i - temp] != 32)                {                    ++temp;                }                fin += s.Substring(i - temp + 1, temp ) + " ";                i -= (temp - 1);            }        }        return fin.TrimEnd(' ');    }}

152.152-Maximum Product Subarray-Difficulty: Medium

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

思路

最大连续积

动态规划

最好的办法就是

因为考虑到负负得正的可能性,最小的数可能一下变到最大的数,我们就既要求/存最大数又要求/存最小数

ac之后解锁了解法



public class Solution {    public int MaxProduct(int[] nums) {                if (nums.Length == 0)            return 0;        int min = nums[0];        int max = nums[0];        int temp = max;        for (int i = 1; i < nums.Length; i++)        {            int temp1 = max * nums[i];            int temp2 = min * nums[i];            max = Math.Max(Math.Max(temp1, temp2), nums[i]);            min = Math.Min(Math.Min(temp1, temp2), nums[i]);                temp = Math.Max(temp, max);        }        return temp;    }    }

153.153- Find Minimum in Rotated Sorted Array-Difficulty: Medium


Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

思路

要利用sorted这个已知项,否则这道题就没有意义了

在discuss上找到一个很好的方法

参考:

public class Solution {    public int FindMin(int[] nums) {      <pre><code class="cpp">  <span class="keyword">int</span> l = <span class="number">0</span>, r = nums.size()-<span class="number">1</span>;    <span class="keyword">while</span> (l < r) {        <span class="keyword">int</span> mid = (r-l)/<span class="number">2</span> + l;        <span class="keyword">if</span> (nums[mid] < nums[r])            r = mid;        <span class="keyword">else</span>            l = mid + <span class="number">1</span>;    }    <span class="keyword">return</span> nums[l];</code>
}}


154.154- Find Minimum in Rotated Sorted Array-Difficulty: Hard

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

思路

和上一题一样,不同的就是这道题包括重复数,

照上一道题多一个步骤,如果相等就使rPointer++

public class Solution {    public int FindMin(int[] nums) {   <pre><code class="cs">  <span class="keyword">int</span> l = <span class="number">0</span>, r = nums.length-<span class="number">1</span>;     <span class="keyword">while</span> (l < r) {         <span class="keyword">int</span> mid = (l + r) / <span class="number">2</span>;         <span class="keyword">if</span> (nums[mid] < nums[r]) {             r = mid;         } <span class="keyword">else</span> <span class="keyword">if</span> (nums[mid] > nums[r]){             l = mid + <span class="number">1</span>;         } <span class="keyword">else</span> {               r--;  <span class="comment">//nums[mid]=nums[r] no idea, but we can eliminate nums[r];</span>         }     }     <span class="keyword">return</span> nums[l];</code>
}}


155.155-Min Stack-Difficulty: Easy

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

思路

模拟一个栈

博主使用一个数组模拟了栈的行为,leetcode的测试数目最多是15000还是16000忘了,使数组容量为16001了

public class MinStack {    int[] stack = new int[16001];    int top_num = 0;    public void Push(int x)    {        stack[top_num] = x;        ++top_num;    }    public void Pop()    {        if (top_num > 0)            --top_num;    }    public int Top()    {        if (top_num > 0)            return stack[top_num - 1];        return 0;    }    public int GetMin()    {        if (top_num <= 0)            return 0;        int min = stack[0];        for (int i = 1; i < top_num; i++)            if (min > stack[i]) min = stack[i];        return min;    }}






1 0
原创粉丝点击