学习第五天

异或巧妙解决数组问题

异或是将数字转换成二进制后进行运算,其结果是如果两个相同的数异或等于0,0和任何数异或都等于本身,并且同时异或很多数字满足交换律

只出现一次的数字

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:

输入: [2,2,1]
输出: 1
示例 2:

输入: [4,1,2,1,2]
输出: 4

解决方法

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        a = 0
        for i in nums:
            a = a^i // 所有成对出现的数字异或以后都等于零,零和单个数字异或最后只留下单个数字
            
        return a

缺失数字

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

示例 1:

输入: [3,0,1]
输出: 2
示例 2:

输入: [9,6,4,2,3,5,7,0,1]
输出: 8

方法1:使用异或

class Solution {
    public int missingNumber(int[] nums) {
        int a = 0;
        for(int i=0;i<nums.length+1;i++){
            a = a^i;
        }
        for(int i=0;i<nums.length;i++){
            a = a^nums[i];
        }
        return a;
    }
}

方法2:用一个完整列表的和减去缺失列表的和就等于缺失的数字

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        result_all = 0
        result = 0
        for i in range(len(nums)+1):
            result_all +=i
        for i in nums:
            result +=i
            
        return result_all-result

旋转数组

给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: [1,2,3,4,5,6,7] 和 k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右旋转 1 步: [7,1,2,3,4,5,6]
向右旋转 2 步: [6,7,1,2,3,4,5]
向右旋转 3 步: [5,6,7,1,2,3,4]
思路:使用两个循环,重点在内部循环,内部循环每次开始之前,先保存一个数字用于交换,然后依次使nums[i]=nums[i-1]使数组的数全部向右移动,然后把数组的首端替换为移动之前保存的数字,就完成了数组的一次旋转

class Solution {
    public void rotate(int[] nums, int k) {
        int len = nums.length;
        k = k%len;//最小移动次数
        for (int i=0;i<k ;i++ ) //
        {
            int temp = nums[len-1];//每次移动前保存数组末尾数字,移动完成后再添加到数组最前面
            for (int j=len-1;j>0 ;j-- )//每执行一次内循环,数组的所有位置就往右移一次
            {
                nums[j] = nums[j-1];
            }
            nums[0] = temp;//一次移动完成
        }
    }
}

买卖股票的最佳时机

只要明天的价格比今天高就今天买明天卖

class Solution {
    public int maxProfit(int[] prices) {
        int max = 0;
        for (int i=0; i<prices.length-1;i++){
            if (prices[i]<prices[i+1]){
                max +=prices[i+1] - prices[i];
            }
        }
        return max;
    }
}

JavaScript操作记录

常用操作是在<script></script>标签中定义事件函数,然后在<body>中定义触发源

如添加表单验证功能

表单提交事件是<from>标签里面的onsubmit属性,并且调用的函数要有返回值,返回true允许提交;输入框失去焦点事件是<input>标签里面的onblur属性
向节点中写入数据用document.getElementById("pwdwarning").innerHTML = " <font color='red'>请输入6-16位密码</font>"使用<font>标签直接括住文本,可以输出任意格式文本,注意灵活使用单引号和双引号,避免发生歧义
修改表单内容可以使用document.getElementById("pwdwarning").value ="修改表单内容"

<script type="text/javascript">

            function checkForm(){
                if (checkuser()&&checkpwd()&&checkrepwd()){
                    alert("注册成功");
                    return true;
                }
                return false;
            }
            
            
            function checkpwd(){
                 pwd = document.getElementById("pwd").value;
                 if (pwd.length<6 || pwd.length>15){
                    document.getElementById("pwdwarning").innerHTML = " <font color='red'>请输入6-16位密码</font>";
                    return false;
                 }
                 else{
                    document.getElementById("pwdwarning").innerHTML = "  <font color='green'>密码符合要求</font>";
                    return true;
                 }
            }
            
            function checkuser(){
                 user = document.getElementById("user").value;
                 if (user.length<6 || user.length>15){
                    document.getElementById("userwarning").innerHTML = " <font color='red'>请输入有效用户名</font>";
                    return false;
                 }
                 else{
                    document.getElementById("userwarning").innerHTML = "  <font color='green'>用户名可以使用</font>";
                    return true;
                 }
            }
            
            function checkrepwd(){
                 repwd = document.getElementById("repwd").value;
                 pwd = document.getElementById("pwd").value;
                 if (repwd!=pwd){
                    document.getElementById("repwdwarning").innerHTML = " <font color='red' >两次输入不一致</font>";
                    return false;
                 }
                 return true;
//               else{
//                  document.getElementById("pwdwarning").innerHTML = "  <font color='green'>密码符合要求</font>";
//               }
            }
        </script>
<body>
<form onsubmit="return checkForm()" action="https://www.baidu.com" method="get">
                        <table border="3" cellspacing="" cellpadding="" width="50%" align="center" bgcolor="#FFFFFF">
                            
                            
                            <tr><td id="xxx">用户名</td> <td width="80%"><input type="text" name="user" id="user" onblur="checkuser()"/>
                            <span id="userwarning"></span> <!--用户名输入提示-->
                            </td></tr>
                            
                            <tr><td>密码</td> <td><input type="password" name="pwd" id="pwd" onblur="checkpwd()"/>
                            <span id="pwdwarning"></span>
                            </td></tr>

                            <tr><td>确认密码</td> <td><input type="password" name="repwd" id="repwd" onblur="checkrepwd()"/>
                            <span id="repwdwarning"></span>
                            </td></tr>
                            <tr><td>性别</td> <td><input type="radio" name="sex" id="" value="" />男<input type="radio" name="sex" id="" value="" />女</td></tr>
                            <tr><td>爱好</td> <td><input type="checkbox" name="like"/>篮球<input type="checkbox" name="like"/>
                                                足球<input type="checkbox" name="like"/>排球<input type="checkbox" name="like"/>羽毛球</td></tr>
                            <tr><td>籍贯</td> <td><select name="">
                                                <option value="">北京</option>
                                                <option value="">上海</option>
                                                <option value="">广州</option>
                                                <option value="">深圳</option>
                                                </select></td></tr>
                            <tr><td>头像</td> <td><input type="file" name="" id="" value="" /></td></tr>
                            <tr><td>简介</td> <td><textarea name="" rows="5" cols="">我是:</textarea></td></tr>
                            <tr><td>生日</td> <td><input type="date" name="" id="" value="" /></td></tr>
                            <tr ><td colspan="2"><input type="submit" value="注册" /><input type="button" value="重置" /><input type="button" value="我是按钮" /></td></tr>
                        </table>
                    </form>
</body>

猜你喜欢

转载自www.cnblogs.com/zhz-8919/p/10624175.html