面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
package com.Amazon.interview; /**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2014年10月25日下午5:14:33
*
************* function description ***************
*
* Question:
*
* Given an array with positive integers and another integer for
* example{7 2 4} 9, you are required to generate an equation, by
* inserting operator add ("+") and minus ("-") among the array . The
* left side of equation are consist of the array and the right side of
* equation is the integer. here the result is 7-2+4=9
*
*
*
* Rules:
*
* Don't include any space in the generated equation. In case there is no
* way to create the equation, please output "Invalid". For example {1 1}
* 10, output is "Invalid"
*
* There is no operator "+" or "-" in front of the first number: Don't
* change the order of the numbers. For example: {7 2 4} 9. 7-2+4=9 is
* correct answer, 4-2+7=9 is wrong answer. There could be multiple
* input, meaning your function could be called multiple times. Do
* remember print a new line after the call.
*
* The length of the integer array is from 1 to 15( include 1 and 15). If
* the length is 1, for example the input {7} 7, the output is 7=7
*
* Sample Input and Output:
*
* Input:
*
* 1 2 3 4 10
*
* 1 2 3 4 5
*
* Output:
*
* 1+2+3+4=10
*
* Invalid
*
*
*
****************************************************
*/ public class GenerateEquation {
public static void createEqualAndPrint(int[] a, int n, int target) {
if (a == null || a.length == 0 || a.length != n || n == 0) {
System.out.println("Invalid"); return;
} if (n < 1 || n > 15) {
System.out.println("Invalid");
return;
} int i = n - 1;
int v = 1;
while (i > 1) {
v = (v << 1) + 1;
i--;
} int sum = 0;
String s = null;
while (v > 0) {
sum = a[0];
s = a[0] + "";
for (int j = n - 1; j > 0; j--) {
int c = v >> (j - 1);
if ((c & 1) == 1) {
sum += a[n - j];
s += "+" + a[n - j];
} else {
sum -= a[n - j];
s += "-" + a[n - j];
}
}
if (sum == target) {
System.out.println(s + "=" + target);
return;
} else {
v--;
}
} System.out.println("Invalid.");
} /**
* @param args
*/
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4 }; createEqualAndPrint(a, 4, 2);
} }
面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0的更多相关文章
- VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同
1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...
- Maven实战错误笔记:使用mvn archetype:generate报错:Unable to add module to the current project as it is not of packaging type 'pom'
在使用mvn archetype:generate生成Maven实战03:HelloWorld中的HelloWorld的项目骨架时报了这个错,从字面上分析是可能与pom.xml文件有关,然后我看了一下 ...
- Idiomatic Python手记一: average in FP way
方法一: import operator def average(*args): return reduce(operator.add, args) / len(args) if args else ...
- Delphi泛型动态数组的扩展--转贴
此文章转载于http://www.raysoftware.cn/?p=278&tdsourcetag=s_pcqq_aiomsg的博客 从Delphi支持泛型的第一天起就有了一种新的动态数组类 ...
- [Hive - Tutorial] Querying and Inserting Data 查询和插入数据
Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...
- C/C++ 笔试题
/////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...
- C/C++笔试题(很多)
微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...
- python公司面试题集锦 python面试题大全
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...
- 【转】C/C++程序员应聘常见面试题深入剖析
1.引言 本文的写作目的并不在于提供C/C++程序员求职面试指导,而旨在从技术上分析面试题的内涵.文中的大多数面试题来自各大论坛,部分试题解答也参考了网友的意见. 许多面试题看似简单,却需要深厚的基 ...
随机推荐
- 【玩转微信公众平台之六】 搭建新浪SAEserver
赶紧接上一篇继续讲. ------本篇将介绍怎样搭建 新浪SAEserver.猛戳 http://sae.sina.com.cn/1.先自己注冊一个账号,假设有新浪的账号,微博之类的都能够直接拿来用, ...
- 使用json-lib进行Java和JSON之间的转换
1. json-lib是一个java类库,提供将Java对象,包括beans, maps, collections, java arrays and XML等转换成JSON,或者反向转换的功能. 2. ...
- The Tangled Web (Web之困)第四章 摘要
1. HTML语法: 由Tag组成层级结构,标签视为名,而值插在当中. 关键组成符:<, >, ', ", & 2. 解析模式: 文件开头<!DOCTYPE> ...
- DHCPv6 server port and DHCPv6 client port
The DHCPv6 draft says that, the port numbers for client and agents are given as 546 & 547 在wires ...
- PHP第四章数组2
$str =array("dd"=>"d","dc"=>"ds","dd"=>&q ...
- java反射技术
Class c2 = Class.forName("com.reflection.Test"); // 对类的寻找,找到一个类,注意不是对象 WifiManager mWifiMa ...
- VIM学习1
不得不说鸟哥的Linux写得太好了,VIM篇章,通读一篇,感觉收获挺大.之前几年前装逼硬着学,感觉硬是没懂,看的特晕,学得特别慢,抄一两遍也没什么多大的作用.这一回看了,感觉马上就能记住不少,当然大多 ...
- Android开发之Adapter
学习android时,对于我这种初学者来说,刚开始接触控件,发现有的控件需要adapter有些不需要,对此我感到不解.所以决定一探究竟. 其实android是一个完全遵从MVC模式的框架,activi ...
- MVC小系列(十)【PartialView中的页面重定向】
在mvc的每个Action中,都可以指定一种返回页面的类型,可以是ActionResult,这表示返回的页面为View或者是一个PartialView, 在以Aspx为页面引擎时,PartialVie ...
- JDK版本过高,导致Eclipse报错
1.JDK版本如果比较高,而使用的eclipse版本比较低,导致在eclispe中不能识别而报错. 2.点击Attach Source添加rt.jar后,又出现如下错误 3.这样的错误就是由于ec ...