1. 1-bit and 2-bit Characters

We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).

Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.

Example 1:

Input:
bits = [1, 0, 0]
Output: True
Explanation:
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character. 

Example 2:

Input:
bits = [1, 1, 1, 0]
Output: False
Explanation:
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.

Note:

  • 1 <= len(bits) <= 1000.
  • bits[i] is always 0 or 1.

解题思路:找规律,可能出现的数字只有三种组合 0,10,11。所以从左往右扫描输入数字串的时候,从头开始,如果第一位是0,那么断定只有1个bit,如果是1,则断定是2个bit,这样去掉已经判别好的继续往右边扫描(其实可以看成每次都从头扫描)判断即可。

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in); //将输入字符串转为int数组
String input=sc.nextLine();
int n=input.length();
int[] bits=new int[n];
for(int i=0; i<n; i++){
bits[i]=input.charAt(i)-'0';
} boolean result=isOneBitCharacter(bits);
System.out.println(result);
}
} public static boolean isOneBitCharacter(int[] bits){
int i=0; //这里i是数组下标,需要不断往后扫描
while(i<bits.length-1){
i=i+bits[i]+1;
}
return i==bits.length-1;
}
}

总结:这题是众多字符串找规律题目中的一个,找到规律即可,不需要利用什么贪心,动态规划等思路。

2. 132 Pattern

Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.

Note: n will be less than 15,000.

Example 1:

Input: [1, 2, 3, 4]

Output: False

Explanation: There is no 132 pattern in the sequence.

Example 2:

Input: [3, 1, 4, 2]

Output: True

Explanation: There is a 132 pattern in the sequence: [1, 4, 2].

Example 3:

Input: [-1, 3, 2, 0]

Output: True

Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

解题思路:可以使用遍历的方法,3层循环从左往右扫描,但是会超时。

方法一是改进的遍历法,对于ai,aj,ak。i<j<K,以中间的 j 开始遍历,对于aj,只要在左边找到一个小于它的ai,在右边找到小于 aj 但大于 ai 的 ak 即可,为了扩大 ak 的搜索范围,只需要和左边小于 aj 中的最小值比较即可,故当 aj 从左往右遍历时,需要记录其中小于 aj 的最小值。

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();

     // 这里用“|”来匹配多个替换,对于中括号,前面要加上转义字符 \\
String str=input.replaceAll(",| |\\]|\\[","");
char[] crs=str.toCharArray();
int[] nums=new int[crs.length];
for(int i=0; i<crs.length; i++){
nums[i]=crs[i]-'0';
}
System.out.println(find132pattern(nums));
} public static boolean find132pattern(int[] nums) {
int min_i = Integer.MAX_VALUE;
for (int j = 0; j < nums.length - 1; j++) {
min_i = Math.min(min_i, nums[j]);
for (int k = j + 1; k < nums.length; k++) {
if (nums[k] < nums[j] && min_i < nums[k])
return true;
}
}
return false;
} }

方法二是Serching intervals(不知道怎么翻译这个。。。),以例子 [5 6 4 7 3 8 2 9] 来说明思路,先转化为一张图:

其实还是从 ai,aj 入手来遍历寻找 ak,估计是 ak 好找(因为限定条件多),从上图来看,有上升的线和下降的线构成,对于每一对上升的线,起点 num[s] 和终点 nums[i-1] 自然构成了 ai 和 aj,而对于 nums[i] 则可能构成 ak,故从左往右扫描,记录遇到的上升线 [num[s],num[i-1]]。对于上升线的后一点 num[i] 开始遍历判断,这个遍历过程中同样要记录遇到的上升线,然后对上升线的后一点和之前所有的上升线对进行比较,如果是下降线的情况直接递增i遍历即可。

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); String str = input.replaceAll(",| |\\]|\\[","");
char[] crs = str.toCharArray(); int[] nums = new int[crs.length];
for(int i = 0; i < crs.length; i++){
nums[i] = crs[i]-'0';
}
System.out.println(find132pattern(nums));
} public static boolean find132pattern(int[] nums) {     //List< int[] a > intervals = new List<>(); 错误写法
List < int[] > intervals = new ArrayList < > ();
int i = 1, s = 0;
while (i < nums.length) {
if (nums[i] <= nums[i - 1]) {
if (s < i - 1)
intervals.add(new int[] {nums[s], nums[i - 1]});
s = i;
}
for (int[] a: intervals)
if (nums[i] > a[0] && nums[i] < a[1])
return true;
i++;
}
return false;
}
}

方法三是利用栈 (Stack),入手点还是通过 ai,aj 限定 ak 来考虑。有点复杂,较难叙述,还是直接看官方解释吧:https://leetcode.com/problems/132-pattern/solution/

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); String str = input.replaceAll(",| |\\]|\\[","");
char[] crs = str.toCharArray(); int[] nums = new int[crs.length];
for(int i = 0; i < crs.length; i++){
nums[i] = crs[i]-'0';
}
System.out.println(find132pattern(nums));
} public static boolean find132Pattern(int[] nums){
if (nums.length < 3)
return false;
Stack < Integer > stack = new Stack < > ();
int[] min = new int[nums.length];
min[0] = nums[0];
for (int i = 1; i < nums.length; i++)
min[i] = Math.min(min[i - 1], nums[i]);
for (int j = nums.length - 1; j >= 0; j--) {
if (nums[j] > min[j]) {
while (!stack.isEmpty() && stack.peek() <= min[j])
stack.pop();
if (!stack.isEmpty() && stack.peek() < nums[j])
return true;
stack.push(nums[j]);
}
}
return false;
}
}

3. 3Sum

Given an array S of n integers, are there elements abc in S 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.

For example, given array S = [-1, 0, 1, 2, -1, -4],

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

解题思路:虽然可以用遍历的方法来穷举所有可能的三元组,但是时间复杂度会达到O(n3),肯定会超时的。一个可行的方式是,先将数组由小到大排序一遍,从第一个数字开始遍历,将其作为基准,剩下的两个数和与其相反数相同即可。因为整个数组是排号序的,所以可以借鉴快排的思想来使寻找剩下两个数的时间复杂度达到O(n),这样整个算法下来时间复杂度便是O(n2)。具体做法是这样,从基准数字的后一个数字和数组的最后一个数字开始索引,如果其和小于基准数字则将左边索引加1,如果大于则将右边索引减1往左移即可,如果等于基准数字的相反数则即是我们要找的数字对,此时要将左右索引各向右左移动一位,如果和之前的索引数字相等还要继续左/右移索引(为了去重),因为左边增大右边缩小这样剩下的数字中还有可能和基准数字构成 3Sum 数字对。在寻找完与基准数字所有相对应的 3Sum 数字对后,将基准数字后移一位重复此步骤。左右两边这样移动到最后直至左索引大于右索引时还不等于基准数字的相反数则表明基准数字不出现在 3Sum 数字对中,后移基准数字继续寻找。还要一点要注意的是因为题目中说了不能出现重复的数字对,所以在后移基准数字时要和前一个基准数字比较,如果相等则直接右移基准数字。

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine(); //首先将输入的字符串转为int数组,这里有正负号,所以还是先变为字符串数组再转为int数组
String str=input.replaceAll(" |\\]|\\[","");
String[] strs=str.split(",");
int[] nums=new int[strs.length];
for(int i=0;i<strs.length;i++){
nums[i]=Integer.parseInt(strs[i]);
} for(List<Integer> pair: find3Sum(nums)){
for(int i : pair){
System.out.print(i+" ");
}
System.out.println();
}
} public static List<List<Integer>> find3Sum(int[] num){
    // 利用 Arrays 自带的排序方法
Arrays.sort(num);
List<List<Integer>> res = new LinkedList<>();
for (int i = 0; i < num.length-2; i++) {
if (i == 0 || (i > 0 && num[i] != num[i-1])) {
int lo = i+1, hi = num.length-1, sum = 0 - num[i];
while (lo < hi) {
if (num[lo] + num[hi] == sum) {
              //这里直接用 Arrays.asList 方法直接将数字对转为List集合
res.add(Arrays.asList(num[i], num[lo], num[hi]));
while (lo < hi && num[lo] == num[lo+1]) lo++;
while (lo < hi && num[hi] == num[hi-1]) hi--;
lo++; hi--;
} else if (num[lo] + num[hi] < sum) lo++;
else hi--;
}
}
}
return res;
}
}

LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  5. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  7. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  8. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  9. leetcode解题报告(13):K-diff Pairs in an Array

    描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs i ...

随机推荐

  1. 2-SAT入门

    大概学了一下2-SAT,写了一道模板和一道USACO 输出一个方案的话,tarjan缩点后倒着拓扑,染色输出. 求任何解下选哪个就得枚举每个点dfs来判断选哪个. HIT 1917(2-sat模板) ...

  2. poj:1850 Code(组合数学?数位dp!)

    题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...

  3. MongoDB插入数据的3种方法

    insert()方法: 下面是在inventory集合中插入一个三个字段的文档: db.inventory.insert( { _id: 10, type: "misc", ite ...

  4. Codeforces Round #341 (Div. 2)A

    A. Wet Shark and Odd and Even time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. [nginx]nginx rewrite规则之last和break

    c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...

  6. Leetcode 001. 两数之和(扩展)

    1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...

  7. 通过psexec实现远程安装软件包

    1.在管理机上下载和安装psexec https://docs.microsoft.com/en-us/sysinternals/downloads/psexec 2.在管理机上编写bat脚本,存放在 ...

  8. Hibernate入门(4)- Hibernate数据操作

    Hibernate加载数据 Session.get(Class clazz, Serializable id) clazz:需要加载对象的类,例如:User.class id:查询条件(实现了序列化接 ...

  9. 【设计模式】 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  10. 【NOI】2004 郁闷的出纳员

    [算法]平衡树(treap) [题解] treap知识见数据结构. 解法,具体细节见程序. #include<cstdio> #include<algorithm> #incl ...