leetcode1,7,9,14,20

本文解析了LeetCode上的经典题目,包括两数之和、反转整数、回文数、最长公共前缀及有效括号等,提供了多种解题思路与代码实现。

 Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

给一个数组nums找出和为target的元素索引

方法一 循环遍历

  public int[] twoSum(int[] nums, int target) {
	        int[] res  = new int[2];
	        for(int i=0;i<nums.length;i++)
	        {
	            for(int j=i+1;j<nums.length;j++)
	            {
	                if((nums[i]+nums[j])==target)
	                {
	                    res[0]=i;
	                    res[1]=j;
	                    return res;
	                }
	            }
	        }
	        return res;
	
	    }
	

复杂度n^2

方法二 

        用map记录元素对应索引

  public int[] twoSum(int[] nums, int target) {
	      
		  int[] res  = new int[2];
		  Map<Integer,Integer> map  = new HashMap<>();
		  for(int i=0;i<nums.length;i++)
		  {
			  if(map.containsKey(target-nums[i]))
			  {
				  res[0]=map.get(target-nums[i]);
				  res[1]=i;
				  return res;
			  }
			  else
			  {
				  map.put(nums[i], i);
			  }
				
		  }
		  return res;
	    }

复杂度n

Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

将数子倒过来

public int reverse(int x) {
        int res=0;
        while(x!=0)
        {
            int tail=x%10;//取个位
            int newres=res*10+tail;//新结果等于上次处理结果*10+个位
            if((newres-tail)/10!=res)//处理溢出 新结果如果能退回老结果则证明无溢出
            {
                return 0;
            }
            res=newres;
            x=x/10;
        }
        return res;
    }

 Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

判断是否是回文数字负数为false

思路一

        先与上一题一样操作将数字反转然后比较反转前后是否相同相同则为回文数字

      

 public boolean isPalindrome(int x) {
        int res=0;
        int xx=x;
       if(x<0)
       {
           return false;
       }
        while(xx!=0)
        {
            int tail=xx%10;
            int newres=res*10+tail;
            if((newres-tail)/10!=res)//处理溢出
            {
                res=0;
                break;
            }
            res=newres;
            xx/=10;
        }
       return res==x?true:false;
    }

思路二

        在思路一上改进只需转换原数字的一半位数

 public boolean isPalindrome(int x) {
         if (x<0 || (x!=0 && x%10==0)) return false;//小于10的数不是回文且无法用算法判断
        int res=0;
        while(x>res)
        {
            int tail=x%10;
            res=res*10+tail;
            x=x/10;
        }
        return res==x||res/10==x;//数字位数分奇偶
    }

Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
找到最长的公共前缀

        思路:假定最长的公共前缀为pre遍历字符串数组每一元素判断每一个元素是否以pre为前缀如果当前字符串不以pre开头则将pre缩短一位判断直到当前字符串以pre开头在判断下一个

 public String longestCommonPrefix(String[] strs) {
   
        if(strs==null||strs.length==0)
      {
          return "";
      }
        String pre=strs[0];
        for(int i=1;i<strs.length;i++)
        {
            while(strs[i].indexOf(pre)!=0)
            {
                pre=pre.substring(0,pre.length()-1);
            }
        }
        return pre;
    }

Valid Parentheses

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

思路一

        建一个栈左括号压栈右括号弹栈匹配

 Stack<Character> st = new Stack<>();
	    public boolean isValid(String s) {
	        char[] chars=s.toCharArray();
	        for(int i=0;i<chars.length;i++)
	        {
	            char ch=chars[i];
	           if(ch=='(')
	           {
	               st.push(ch);
	           }
	           else   if(ch=='{')
	           {
	               st.push(ch);
	           }
	            else if(ch=='[')
	           {
	            	 st.push(ch);
	           }
	            else if(ch==')')
	           {
	               if(st.isEmpty())
	               {
	                   return false;
	               }
	                else
	                {
	                    if(st.pop()!='(')
	                    {
	                        return false;
	                    }
	                }        
	           }
	            
                else if(ch=='}')
	           {
	              if(st.isEmpty())
	               {
	                   return false;
	               }
	                else
	                {
	                    if(st.pop()!='{')
	                    {
	                        return false;
	                    }
	                }
	           }
	            else  if(ch==']')
	           {
	              if(st.isEmpty())
	               {
	                   return false;
	               }
	                else
	                {
	                    if(st.pop()!='[')
	                    {
	                        return false;
	                    }
	                }
	           }
               
                
	            
	        }//
	        if(st.isEmpty()==true)
	        {
	        	return true;
	        }
	        else
	        {
	        	return false;
	        }
	    }

思路二

   public boolean isValid(String s) {
	         Stack<Character> st = new Stack<>();
            char[] chars=s.toCharArray();
	        for(int i=0;i<chars.length;i++)
	        {
	            char ch=chars[i];
	           if(ch=='(')
	           {
	               st.push(')');
	           }
	           else if(ch=='{')
	           {
	               st.push('}');
	           }
	            else if(ch=='[')
	           {
	            	 st.push(']');
	           }
	            else if(st.isEmpty()||st.pop()!=ch)
	           {
	              return false;
	           }
	            
                         
	            
	        }//
	        return st.isEmpty();
	    }

基于遗传算法的微电网调度(风、光、蓄电池、微型燃气轮机)(Matlab代码实现)内容概要:本文档介绍了基于遗传算法的微电网调度模型,涵盖风能、太阳能、蓄电池和微型燃气轮机等多种能源形式,并通过Matlab代码实现系统优化调度。该模型旨在解决微电网中多能源协调运行的问题,优化能源分配,降低运行成本,提高可再生能源利用率,同时考虑系统稳定性与经济性。文中详细阐述了遗传算法在求解微电网多目标优化问题中的应用,包括编码方式、适应度函数设计、约束处理及算法流程,并提供了完整的仿真代码供复现与学习。此外,文档还列举了大量相关电力系统优化案例,如负荷预测、储能配置、潮流计算等,展示了广泛的应用背景和技术支撑。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事微电网、智能电网优化研究的工程技术人员。; 使用场景及目标:①学习遗传算法在微电网调度中的具体实现方法;②掌握多能源系统建模与优化调度的技术路线;③为科研项目、毕业设计或实际工程提供可复用的代码框架与算法参考; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注目标函数构建与约束条件处理,同时可参考文档中提供的其他优化案例进行拓展学习,以提升综合应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值