status
Published
date
Jun 1, 2020
slug
summary
category
算法
tags
  1. 位运算
    1. //链接:https://www.nowcoder.com/questionTerminal/59ac416b4b944300b617d4f7f111b215
      //来源:牛客网
      //Java
      public class Solution {
          public int Add(int num1,int num2) {
              while (num2!=0) {
                  int temp = num1^num2;
                  num2 = (num1&num2)<<1;
                  num1 = temp;
              }
              return num1;
          }
      }
      
      //C++:
      class Solution {
      public
          int Add(int num1, int num2)
          {
              return num2?Add(num1^num2,(num1&num2)<<1):num1;
          }
      };
      
  1. 汇编
    1. //链接:https://www.nowcoder.com/questionTerminal/59ac416b4b944300b617d4f7f111b215
      //来源:牛客网
      int add(int a, int b)
      {
          _asm
          {
              MOV EAX, a
              MOV ECX, b
              ADD EAX, ECX
          }
      }
      
  1. 自增自减
    1. //链接:https://www.nowcoder.com/questionTerminal/59ac416b4b944300b617d4f7f111b215
      //来源:牛客网
      
      public class Solution {
          public int Add(int num1,int num2) {
              if(num1>0){
                  while(num1--!=0)
                      num2++;
              }
              else if(num1<0){
                  while(num1++!=0)
                      num2--;
              }
              return num2;
          }
      }
      
  1. 通过指针
    1. //链接:https://www.nowcoder.com/questionTerminal/59ac416b4b944300b617d4f7f111b215
      //来源:牛客网
      
      class Solution {
      public:
          int Add(int num1, int num2)
          {
              char* a = reinterpret_cast<char*>(num1);
              return reinterpret_cast<long>(&(a[num2]));
          }
      };
      
  1. 通过调用Integer.sum()函数
    1. public int Add(int num1, int num2) {
         return Integer.sum(num1, num2);
      }
      Integer.sum()源码
      public static int sum(int a, int b) {
              return a + b;
      }
       

  • Cusdis
  • GitTalk