1.全排列https://leetcode-cn.com/problems/permutations-ii/
给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:
输入: [ 1 , 1 , 2 ]
输出:
[
[ 1 , 1 , 2 ] ,
[ 1 , 2 , 1 ] ,
[ 2 , 1 , 1 ]
]
思路一:题目要求不能有重复的排列,可以用dfs全排列+set暴力去重,但是效率很低
class Solution {
Set< List< Integer> > set = new HashSet < > ( ) ;
public List< List< Integer> > permuteUnique ( int [ ] nums) {
dfs ( nums, 0 ) ;
List< List< Integer> > ans = new ArrayList < > ( set) ;
return ans;
}
private void dfs ( int [ ] nums, int cur) {
if ( cur == nums. length) {
List< Integer> list = new ArrayList < > ( ) ;
for ( int i : nums) {
list. add ( i) ;
}
set. add ( list) ;
} else {
for ( int i = cur; i < nums. length; i++ ) {
swap ( nums, cur, i) ;
dfs ( nums, cur + 1 ) ;
swap ( nums, cur, i) ;
}
}
}
private void swap ( int [ ] nums, int i, int j) {
int temp = nums[ i] ;
nums[ i] = nums[ j] ;
nums[ j] = temp;
}
}
思路二:在dfs全排列搜索交换前先判断一下是否已经交换过
class Solution {
List< List< Integer> > ans = new ArrayList < > ( ) ;
public List< List< Integer> > permuteUnique ( int [ ] nums) {
dfs ( nums, 0 ) ;
return ans;
}
private void dfs ( int [ ] nums, int cur) {
if ( cur == nums. length) {
List< Integer> line = new ArrayList < > ( ) ;
for ( int i : nums) {
line. add ( i) ;
}
ans. add ( line) ;
} else {
for ( int i = cur; i < nums. length; i++ ) {
if ( canSwap ( nums, cur, i) ) {
swap ( nums, cur, i) ;
dfs ( nums, cur + 1 ) ;
swap ( nums, cur, i) ;
}
}
}
}
private boolean canSwap ( int nums[ ] , int begin, int end) {
for ( int i = begin; i < end; i++ ) {
if ( nums[ i] == nums[ end] ) {
return false ;
}
}
return true ;
}
private void swap ( int nums[ ] , int i, int j) {
int temp = nums[ i] ;
nums[ i] = nums[ j] ;
nums[ j] = temp;
}
}
2.1比特与2比特字符https://leetcode-cn.com/problems/1-bit-and-2-bit-characters/
有两种特殊字符。第一种字符可以用一比特0 来表示。第二种字符可以用两比特( 10 或 11 ) 来表示。
现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0 结束。
示例 1 :
输入:
bits = [ 1 , 0 , 0 ]
输出: True
解释:
唯一的编码方式是一个两比特字符和一个一比特字符。所以最后一个字符是一比特字符。
示例 2 :
输入:
bits = [ 1 , 1 , 1 , 0 ]
输出: False
解释:
唯一的编码方式是两比特字符和两比特字符。所以最后一个字符不是一比特字符。
注意:
1 <= len ( bits) <= 1000.
bits[ i] 总是0 或 1.
思路:定义一个index,遍历,如果是1 的话,说明要组合成2位,这时候index += 2,否则就++即可,如果能过一直进行到最后,index == length 时说明为true
class Solution {
public boolean isOneBitCharacter ( int [ ] bits) {
int index = 0 ;
while ( index < bits. length - 1 ) {
if ( bits[ index] == 1 ) {
index += 2 ;
}
else {
index++ ;
}
}
return index == bits. length - 1 ;
}
}
3.反转链表https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1 - > 2 - > 3 - > 4 - > 5 - > NULL
输出: 5 - > 4 - > 3 - > 2 - > 1 - > NULL
限制:
0 <= 节点个数 <= 5000
思路:三指针,往前走,中间的指向后面的,前面的用来判断。走到最后之后,将头节点置null,second所处位置就是新的头节点
class Solution {
public ListNode reverseList ( ListNode head) {
if ( head == null || head. next == null) {
return head;
}
ListNode first = head;
ListNode second = first. next;
ListNode third = second. next;
while ( third != null) {
second. next = first;
first = second;
second = third;
third = third. next;
}
second. next = first;
head. next = null;
head = second;
return head;
}
}