leetCode

反转整数

class Solution {
    public int reverse(int x)
    {
        boolean negative = x < 0;
        if (negative)
            x = -x;
        long r = 0;
        while (x > 0)
        {
            r = r * 10 + x % 10;
            x /= 10;
        }
        if (negative)
            r = -r;
        if (r > Integer.MAX_VALUE || r < Integer.MIN_VALUE)
            return 0;
        return (int) r;
    }
}

 

回文数 

   public boolean isPalindrome(int x)
    {
        if(x < 0 || (x % 10 == 0 && x != 0)) {//临界问题
           return false; 
        }
        int revertedNumber = 0;
        while(x > revertedNumber) {//反转一半数字
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }
        return x == revertedNumber || x == revertedNumber/10;
       /* int xbak = x;
        int reverse = 0;
        while (x > 0)
        {
            int t = x % 10;
            reverse = reverse * 10 + t;//不好,反转之后可能越界
            x/=10;
        }

        return (reverse == xbak);   */    
    }

最长公共前缀

public class LongestCommonPrefix
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String[] string =
        { "flower", "flow", "flight" };
        String s = longestCommonPrefix(string);
        System.out.println(s);

    }

    public static String longestCommonPrefix(String[] strs)
    {
        if (strs == null || strs.length == 0)//数组判空
        {
            return "";
        }

        String s;
        for (int j = 1; j <= strs[0].length(); j++)
        {
            s = strs[0].substring(0, j);
            for (int i = 1; i < strs.length;)
            {
                if (strs[i].startsWith(s))
                {
                    i++;
                } else
                {
                    return s.substring(0, s.length() - 1);

                }
            }

        }
        return strs[0];

    }

}

实现strStr()

public int strStr(String haystack, String needle)
    {
        if (null == haystack || haystack.length() < needle.length())
        {
            return -1;
        }

        if (null == needle || needle.length() == 0)
        {
            return 0;
        }
        int len = haystack.length() - needle.length() + 1;// 这个是关键,否则可能溢出
        for (int i = 0; i < len; i++)
        {
            String childString = haystack.substring(i);
            if (childString.startsWith(needle))
            {
                return i;
            }
        }
        return -1;
    }

删除排序数组中的重复项

public class Solution1
{

    public static void main(String[] args)
    {
        int[] a = { 1,1,2};
        System.out.println(removeDuplicates(a));

    }

    public static int removeDuplicates(int[] nums)
    {

        if (nums.length == 0)
        {
            return 0;
        }
        int i = 0;  //快慢指针
        for (int j = 1; j < nums.length; j++)
        {
            if (nums[i] != nums[j])
            {
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }

}

有效的括号

/*
     * 基于栈后入先出的特点,我们对左括号([{不停压栈,而处理)]}时, 当栈顶元素与之对应则将其出栈,最后如果栈为空,
     * 则表明所有({[都已出栈;而如果栈顶元素与)]}错误匹配如(}则肯定无法闭合,直接return false即可。
     */
    public boolean isValid(String s)
    {
        if (s == null || s.length() == 0)
        {
            return true;
        }
        if (s.length() % 2 == 1)
        {
            return false;
        }
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++)
        {
            if (stack.isEmpty())
            {
                stack.push(s.charAt(i));
            } else if (stack.peek() == '(' && s.charAt(i) == ')')
            {
                stack.pop();
            } else if (stack.peek() == '[' && s.charAt(i) == ']')
            {
                stack.pop();
            } else if (stack.peek() == '{' && s.charAt(i) == '}')
            {
                stack.pop();
            } else
            {
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();
    }

 

最大子序和

 public static void main(String[] args)
    {
        int[] a ={ -22, -10, -3, -4, -11, -29, -10, -5, -6 };
        System.out.println(maxSubArray(a));

    }

    public static int maxSubArray(int[] nums)//主要是算法思想
    {
        int sum = nums[0];
        int current = nums[0];
        int len = nums.length;
        for (int i = 1; i < len; i++)
        {
            if (current > 0)
            {
                current += nums[i];
            } else
            {
                current = nums[i];
            }
            if (sum < current)
            {
                sum = current;
            }
        }

        return sum;

    }

 

 

二进制求和


import java.util.Stack;

public class Solution7
{
    public static void main(String[] args)
    {
        System.out.println(toTen("11"));
    }

    public String addBinary(String a, String b)
    {
        /*   溢出
          int int_a = 0;
          int int_b = 0; 
          int result = 0; 
          int_a = toTen(a); 
          int_b = toTen(b); 
          result = int_a + int_b; 
          String str_result = toBinaryString(result); return str_result;
         */
        
        /*
         * 这个方法好
         */
        int i = a.length() - 1, j = b.length() - 1, carry = 0;
        StringBuilder sb = new StringBuilder();
        while(i >=0 || j >=0){
            int m = i >= 0 ? a.charAt(i) - '0' : 0;
            int n = j >= 0 ? b.charAt(j) - '0' : 0;
            int sum = m + n + carry;
            carry = sum / 2;
            sb.insert(0, String.valueOf(sum % 2));
            i--;
            j--;
        }
        if(carry != 0) sb.insert(0, '1');
        return sb.toString();
    }

    /*
     * 2进制转10进制
     */
    public static int toTen(String a)
    {

        int int_a = 0;
        int len = a.length();
        for (int i = 0; i < len; i++)
        {
            int c = Integer.valueOf(a.charAt(i) + "");
            int_a += c * Math.pow(2, len - i - 1);
        }
        return (int) int_a;
    }

    /*
     * 10进制转2进制字符
     */
    public static String toBinaryString(int d)
    {
        Stack<Integer> stack = new Stack<Integer>();
        StringBuffer sBuffer = new StringBuffer();
        while (d >= 2)
        {
            int mod = d % 2;
            d /= 2;
            stack.push(mod);
        }
        stack.push(d);
        while (!stack.isEmpty())
        {
            sBuffer.append(stack.pop());

        }
        return sBuffer.toString();
    }
}

 

 

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值