solidity简单算法实现

233 阅读1分钟

分享一下最近用solidity写的三个算法(包括链表的设计),主要目的就是熟悉一下solidity的底层,写这些算法的过程中发现了自己以前没注意到的细节,有助于以后的开发,大家也可以用这种方式尝试一下开发能力的提高。

注意点!!!:返回值为不定长数组的时候,可以用以下手段解决定长和不定长类型不一致的问题

image.png

问题解答与图片来源:梁培利老师
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Leetcode {
    //1. 两数之和
    // 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。
    // 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
    // 你可以按任意顺序返回答案。
    // 链接:https://leetcode.cn/problems/two-sum/?favorite=2cktkvj
    // function twoSum(uint[] memory nums, uint target) public pure returns(uint[2] memory array) {
    //     for(uint i = 0; i < nums.length; i++){
    //         for(uint j = i + 1; j < nums.length; j++){
    //             if((nums[i] + nums[j]) == target){
    //                 array[0] = i;
    //                 array[1] = j;
    //                 return array;
    //             }
    //         }
    //     }        
    // }
    //2. 两数相加:
    // 给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
    // 请你将两个数相加,并以相同形式返回一个表示和的链表。
    // 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
    // 链接:https://leetcode.cn/problems/add-two-numbers/?favorite=2cktkvj
    // struct Node{
    //     uint val;
    //     bytes32 next;
    // }
    // uint ListNode1Length = 0;
    // uint ListNode2Length = 0;
    // mapping(bytes32 => Node) ListNodel1;
    // mapping(bytes32 => Node) ListNodel2;
    // function initListNodel1(uint[] memory list) public {                        //初始化链表1
    //     ListNodel1[keccak256(abi.encodePacked("first"))] = Node(0, keccak256(abi.encodePacked(ListNode1Length)));
    //     for (uint i = 0; i < list.length; i++){
    //         bytes32 nowRandom = keccak256(abi.encodePacked(ListNode1Length));
    //         ListNode1Length ++;
    //         bytes32 nextRandom = keccak256(abi.encodePacked(ListNode1Length));
    //         ListNodel1[nowRandom] = Node(list[i], nextRandom);
    //     }
    // }
    // function initListNodel2(uint[] memory list) public {                        //初始化链表2
    //     ListNodel2[keccak256(abi.encodePacked("first"))] = Node(0, keccak256(abi.encodePacked(ListNode2Length)));
       
    //     for (uint i = 0; i < list.length; i++){
    //         bytes32 nowRandom = keccak256(abi.encodePacked(ListNode2Length));
    //         ListNode2Length ++;
    //         bytes32 nextRandom = keccak256(abi.encodePacked(ListNode2Length));
    //         ListNodel2[nowRandom] = Node(list[i], nextRandom);
    //     }
    // }
   
    // function addTwoNumber() public view returns(uint[] memory) {
    //     uint maxLength = (ListNode1Length > ListNode2Length) ? ListNode1Length : ListNode2Length;
    //     uint minLength = (ListNode1Length > ListNode2Length) ? ListNode2Length : ListNode1Length;
    //     uint[] memory array =new uint[](maxLength + 1);
    //     bytes32 List1Random = ListNodel1[keccak256(abi.encodePacked("first"))].next;
    //     bytes32 List2Random = ListNodel2[keccak256(abi.encodePacked("first"))].next;
    //     for (uint i = 0; i < maxLength; i++){
    //         if (i == minLength && minLength == ListNode1Length){
    //             for (uint j = i; j < maxLength; j++){
    //                 array[j] = ListNodel2[List2Random].val;
    //                 List2Random = ListNodel2[List2Random].next;
    //             }
    //             break;
    //         } else if (i == minLength && minLength == ListNode2Length){
    //             for (uint j = i; j < maxLength; j++){
    //                 array[j] = ListNodel1[List1Random].val;
    //                 List1Random = ListNodel1[List1Random].next;
    //             }
    //             break;
    //         }
    //         array[i] = ListNodel1[List1Random].val + ListNodel2[List2Random].val;
    //         List1Random = ListNodel1[List1Random].next;
    //         List2Random = ListNodel2[List2Random].next;
    //     }
    //     for (uint a = 0; a < array.length; a++){
    //         if (array[a] >= 10){
    //             array[a] -= 10;
    //             array[a + 1] += 1;
    //             }            
    //     }
    //     if (array[array.length - 1] == 0){
    //         uint[] memory newarray = new uint[](array.length - 1);
    //         for (uint a = 0; a < array.length - 1; a++){
    //             newarray[a] = array[a];
    //         }
    //         return newarray;
    //     }
    //     return array;
    // }
    //3. 无重复字符的最长子串
    // 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
    // 链接:https://leetcode.cn/problems/longest-substring-without-repeating-characters/?favorite=2cktkvj
    // mapping(bytes1 => bool) public substring;  
    // uint public maxLength = 0;  
    // uint public length = 0;  
    // uint public start = 0;
    // function lengthOfLongestSubstring(string memory s) public returns(uint) {  
    //     bytes memory newString = bytes(s);
    //     for (uint i = 0; i < newString.length; i++) {
    //         if (substring[newString[i]] == false) {  
    //             substring[newString[i]] = true;  
    //             length++;
    //             if (length > maxLength) {
    //                 maxLength = length;
    //             }
    //         } else {  
    //             while (newString[start] != newString[i]) {  
    //                 substring[newString[start]] = false;  
    //                 start++;  
    //                 length--;  
    //             }  
    //             start++;  
    //         }  
    //     }
    //     return maxLength;  
    // }  
}