归纳0-错题本-操作技巧
作者:互联网
1-排序的用法
- 实现数据的顺序排列;
- 在数据有重复时,实现相同数据的聚集(能这样做的前提是,最终结果与原先的顺序无关)。
2-数字字典序
import java.util.ArrayList;
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> lexicalOrder(int n) {
int ans = 1;
for(int i=0; i<n; i++){
res.add(ans);
if(ans * 10 <= n){
ans *= 10;
}else{
while(ans%10==9 || ans+1>n){
ans /= 10;
}
ans++;
}
}
return res;
}
}
2-1-字典序,逻辑并不像自己想的那么好写
import java.util.HashMap;
class Solution {
public boolean isAlienSorted(String[] words, String order) {
HashMap<Character, Integer> cache = new HashMap<>();
for(int i=0; i<order.length(); i++){
cache.put(order.charAt(i), i);
}
for(int i=0; i<words.length-1; i++){
String word1 = words[i];
String word2 = words[i+1];
int length = Math.min(word1.length(), word2.length());
flag:{
for(int j=0; j<length; j++){
if(word1.charAt(j)==word2.charAt(j)){
continue;
}else if(cache.get(word1.charAt(j))>cache.get(word2.charAt(j))){
return false;
}else{
break flag;
}
}
if(word1.length()>word2.length()){
return false;
}
}
}
return true;
}
}
3-借助StringBuilder进行快速序列化(int和char之间的转换)
//序列化
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
postOrder(root, sb);
//System.out.println(sb.toString());
return sb.toString();
}
void postOrder(TreeNode root, StringBuilder sb){
if(root==null){
return;
}
postOrder(root.left, sb);
postOrder(root.right, sb);
sb.append((char)root.val); //利用java自带的字符集,进行int->char->string的快速转换
}
//反序列化
public TreeNode deserialize(String data) {
//System.out.println(dataArray[0]);
LinkedList<Integer> dataList = new LinkedList<>();
for(int i=0; i<data.length(); i++){
dataList.add((int)data.charAt(i)); //利用java自带的字符集,进行string->char->int的快速转换
}
//System.out.println(dataList.toString());
return dePostOrder(Integer.MIN_VALUE, Integer.MAX_VALUE, dataList);
}
TreeNode dePostOrder(int minValue, int maxValue, LinkedList<Integer> dataList){
//System.out.println(dataList.peek());
if(dataList.isEmpty() || dataList.getLast()<minValue || dataList.getLast()>maxValue){
return null;
}
TreeNode root = new TreeNode(dataList.removeLast());
root.right = dePostOrder(root.val, maxValue, dataList);
root.left = dePostOrder(minValue, root.val, dataList);
return root;
}
4-python中global的用法
-
背景:46. 全排列
-
总结:全局变量如果使用global在局部初始化,不要忘记赋初值
-
在类方法的局部函数中进行全局计数,可以采用设置全局变量和设置实例变量(实例变量相当于对象中的全局变量,而且用于对象中的局部函数还不用global、nonlocal修饰),这两种方式。
-
1,一段会报错的代码
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
count = 0 #这里的count只是定义在类的方法中的一个局部变量,不是全局变量(局部变量count有初值0,全局变量count没有)
def backtrack(track, choiceList):
global count #就算这里将count使用global修饰,但是全局变量count未赋值
count += 1 #由于count未赋值,所以报错
print('>'*self.count, track)
if len(track) == length:
result.append(copy.copy(track)) #如果直接使用result.append(track),最后result中的是同一个track
self.count -= 1
print('>'*self.count, 'return', track)
return
for num in choiceList:
track.append(num)
choiceList.remove(num)
backtrack(track, choiceList)
track.remove(num)
choiceList.append(num)
backtrack([], nums)
return result
- 修改方法1
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
global count #将局部变量count设置为全局变量
count = 0 #给全局变量count赋初值
def backtrack(track, choiceList):
global count #将count使用global修饰
count += 1 #由于count有初始赋值,所以不会报错
print('>'*count, track)
if len(track) == length:
result.append(copy.copy(track))
count -= 1
print('>'*count, 'return', track)
return
for num in choiceList:
track.append(num)
choiceList.remove(num)
backtrack(track, choiceList)
track.remove(num)
choiceList.append(num)
backtrack([], nums)
return result
- 修改方法2
count = 0 #这一步相当于设置全局变量count,并赋初值0
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
def backtrack(track, choiceList):
global count #将count使用global修饰
count += 1 #由于count有初始赋值,所以不会报错
print('>'*count, track)
if len(track) == length:
result.append(copy.copy(track))
count -= 1
print('>'*count, 'return', track)
return
for num in choiceList:
track.append(num)
choiceList.remove(num)
backtrack(track, choiceList)
track.remove(num)
choiceList.append(num)
backtrack([], nums)
return result
- 修改方法3
import copy
class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
length = len(nums)
self.count = 0 #将count定义为实例变量
def backtrack(track, choiceList):
self.count += 1 #使用实例变量进行计数
print('>'*self.count, 'track', track)
print('>'*self.count, 'choiceList', choiceList)
if len(track) == length:
result.append(copy.copy(track))
self.count -= 1
print('>'*self.count, 'return', track)
print('>'*self.count, 'result', result)
return
for num in choiceList:
track.append(num)
choiceList.remove(num)
backtrack(track, choiceList)
track.remove(num)
choiceList.append(num)
backtrack([], nums)
print(result)
return result
标签:count,return,技巧,归纳,track,错题,int,num,choiceList 来源: https://www.cnblogs.com/tensorzhang/p/16306825.html