ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

归纳0-错题本-操作技巧

2022-05-24 19:31:06  阅读:164  来源: 互联网

标签:count return 技巧 归纳 track 错题 int num choiceList


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有